For Details of The Program: CLICK HERE
class Q1
{
int n,m, no,
digit, i;
BufferedReader
br=new BufferedReader(new InputStreamReader(System.in));
Q1()
{
digit=0;
}
void accept( )
throws Exception
{
System.out.print("\nEnter 'M':");
m=Integer.parseInt(br.readLine());
System.out.print("\nEnter
'N':");
n=Integer.parseInt(br.readLine());
check();
}
void check( )
{
if(n >100
||m<100 m="">10000)100>
System.out.println("Invalid
Input.");
else
{
while(true)
{
no=0;
for(i=m;i
>0;i=i/10)
no=no+i%10;
if(no==n)
{
System.out.println("\nThe
required number:"+m);
System.out.println("\nTotal
number of digits:"+digit(m));
break;
}
m++;
}
}
}
int digit(int
x)
{
int c=0;
for(int j=x;j
>0;j=j/10)
c++;
return c;
}
public static
void main(String args[]) throws Exception
{
Q1 one=new
Q1();
one.accept();
}
}
Variable
description
Type
|
Variable Name
|
Purpose
|
Scope
|
||||||
int
|
n
|
To store the lower range
|
Entire program
|
||||||
m
|
To store the upper
|
Entire program
|
|||||||
range
|
|||||||||
int
|
no
|
To store the
|
Entire program
|
||||||
nearest
required
|
|||||||||
number
|
|||||||||
Int
|
digit
|
Count the digit
|
Entire program
|
||||||
int
|
i
|
Loop control
|
Entire program
|
||||||
variable
|
|||||||||
int
|
x
|
To hold the value
|
digit function
|
||||||
of m
|
|||||||||
Int
|
c
|
counter
|
digit function
|
||||||
BufferedReader
|
br
|
Input object
|
Entire program
|
||||||
int
|
j
|
Loop control
|
check function
|
||||||
variable
|
|||||||||
Algorithm
|
Step 1: Create
int variables n,m,digit,no,i
Step 2: Take
lower range and upper range values and store in n and m
Step 3: Check
whether n and m are within specified range
Step 4: Repeat
steps 5 and 6 until the required number is reached
Step 5: Take the sum of the digits of ‘m’ and
check it with ‘n’. If match found count the number of digits in it, display
both and go to step 7
Step 6: Increase ‘m’ by 1
Step 7: End
No comments:
Post a Comment