Question

# Given two positive numbers M and N, such that M is between 100 and 10000 and N is less than 100. Find the smallest integer that is greater than M and whose digits add up to N. For example, if M = 100 and N = 11, then the smallest integer greater than 100 whose digits add up to 11 is 119.Write a program to accept the numbers M and N from the user and print the smallest required number whose sum of all its digits is equal to N. Also, print the total number of digits present in the required number. The program should check for the validity of the inputs and display an appropriate message for an invalid input.

```				```
Test your program with the sample data and some random data:
Example 1:
INPUT:
M = 100
N = 11
OUTPUT:
The required number = 119
Total number of digits = 3
Example 2:
INPUT:
M = 1500
N = 25
OUTPUT:
The required number = 1699
Total number of digits = 4
Example 3:
INPUT:
M = 99
N = 11
OUTPUT:
INVALID INPUT
Example 4:
INPUT:
M = 112
N = 130
OUTPUT:
INVALID INPUT
```
```

# Share code with your friends

Code

```				```
Please do not put value of variable 'n' greater than 72 or else you will be waiting for a long time for your output to show because let's say m=100 and n=90 then the smallest number will be 10 digit long integer and you will be searching from 2 digit long integer till you find that 10 digit long integer.

```
```
```				```
import java.util.Scanner;
public class Smallest
{
public static int sumOfDigits(long n)
{
int sum = 0;
long temp=n;
while(temp>0)
{
sum += temp % 10;
temp=temp/10;
}
return sum;
}

public static int countDigits(long n)
{
int countDigits = 0;
long temp=n;
while(temp>0)
{
countDigits++;
temp=temp/10;
}

return countDigits;
}

public static void main(String args[])
{
int n=0,sum=0,count=0,m=0;
long smallest=0;
Scanner sc=new Scanner(System.in);
System.out.print("M = ");
m = Math.abs(sc.nextInt());
System.out.print("N = ");
n = Math.abs(sc.nextInt());
if(n < 100 && m >= 100 && m <= 10000)
{
sum = 0;
smallest =m;

while(sum != n)
{
sum = sumOfDigits(smallest);
smallest++;
}

System.out.println("The required number = " + smallest);
count = countDigits(smallest);
System.out.println("Total number of digits = " + count);
}
else
{
System.out.println("INVALID INPUT");
}
}

}

```
```

## Coding Store

Sale ### Mastering String

50
Sale 150
Sale 200
Sale ### Mastering Recursion

50
Sale ### Mastering Numbers

50
Sale ### Mastering Matrix

50
Sale ### Mastering Array

50
Sale ### ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)

50
Sale 50