### Computer Science ISC 2015

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 some sample data and some random data:

**Example 1:**

**INPUT :** M= 100

N=11

**OUTPUT:**

The required number is= 119

Total number of digits=3

**Example 2**

**INPUT:** M= 1500

N=25

**OUTPUT:**

The required number is =1699

Total number of digits=4

**Example 3**

**INPUT:** M= 99

N=11

**OUTPUT:**

INVALID INPUT

**Example 4**

INPUT: M= 112

2

N=130

OUTPUT:

INVALID INPUT

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import java.util.*; public class SumDNext { int sum(int n) { int s=0; while(n>0) { s = s+n%10; n = n/10; } return s; } int count(int n) { int c=0; while(n>0) { c++; n = n/10; } return c; } public static void main() { Scanner sc = new Scanner(System.in); int M, N; System.out.println("Enter the Value of M: "); M = sc.nextInt(); System.out.println("Enter the Value of N: "); N = sc.nextInt(); if(M<100||M>10000||N>=100) { System.out.println("INVALID INPUT"); return; } SumDNext ob = new SumDNext(); for(M=M+1;ob.sum(M)!=N;M++); System.out.println("The required number is= "+M); System.out.println("Total number of digits= "+ob.count(M)); } } |

Dedicated server says

The important questions and previous year questions in Computer Science and their solutions are sufficient enough to score cent percent marks in both practical and theory papers. I have been following this site for nearly one year or so. During which I found this site extremely beneficial and helpful.