**Click here to download the complete ISC 2015 Computer Science Paper 2 (Practical).**

#### 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

#### Programming Code:

/** * The class Q1_ISC2015 inputs two integers 'm' and 'n' and prints smallest integer * greater than 'm' whose sum of digits is equal to 'n' * @author : www.guideforschool.com * @Program Type : BlueJ Program - Java * @Question Year : ISC Practical 2015 Question 1 */ import java.util.*; class Q1_ISC2015 { int sumDig(long n) // Function to find sum of digits of a number { int sum = 0, d; while(n>0) { d = (int)(n%10); sum = sum + d; n = n/10; } return sum; } int countDig(long n) // Function to count the number of digits in a number { String s = Long.toString(n); int len = s.length(); return len; } public static void main()throws Exception { Q1_ISC2015 ob = new Q1_ISC2015(); Scanner sc = new Scanner(System.in); System.out.print("Enter a value of 'm' from 100 to 10000 : "); int m = sc.nextInt(); System.out.print("Enter a value of n from 1 to 99 : "); int n = sc.nextInt(); if(m<100 || m>10000 || n<1 || n>99) { System.out.println("Invalid Input"); } else { long i = (long)m; // Required number can be out of range of 'int' /* The required number must be greater than 'm', so loop will go on as long as that number is not obtained.*/ while(ob.sumDig(i)!=n) { i=i+1; } System.out.println("The required number = "+i); System.out.println("Total number of digits = "+ob.countDig(i)); } } }

#### Output:

Enter a value of ‘m’ from 100 to 10000 : 1500

Enter a value of ‘n’ from 1 to 99 : 25

The required number = 1699

Total number of digits = 4

Enter a value of ‘m’ from 100 to 10000 : 100

Enter a value of ‘n’ from 1 to 99 : 20

The required number = 299

Total number of digits = 3

Enter a value of ‘m’ from 100 to 10000 : 112

Enter a value of ‘n’ from 1 to 99 : 130

Invalid Input

I did not use long….i did the program with int…will marks be deducted?

No

sir will marks be deducted for not writing the comments???

1-2 marks may be deducted overall for all programs.

Sir,

I’ve used the for loop to generate values from N till 15000 and calculated the sum of digits and checked if it was equal to N.The output came out perfectly,yet I have a doubt if 15000 is going cause any problem….?!

Yes, it may cause problem. Because for higher values of n, the required number could be more than 15000. But at the end, it all depends on the visiting examiner.

I was able to execute this this program entirly right in the practical ISC and were also able to answer the viva questions.Thanks a lot….Sir

I’m sure of scoring full marks in this paper…Kudos to your entire team for this vast repository of help for students…Great work…The Practical Tips helped me a lot…

sir i attempted this question but i did not write the method for returning the no. of digits

but my program is working perfectly on the computer with the given outputs in the specified format

will there be some deductions??

No.

Sir, I have attempted the first program during the ISC Computer Science Practical Exam.I have solved the entire program within a single method, and have not included any main method in the program.Though the output has been flawless each time the program was executed, this makes me worry if I am to lose marks for making the program too simple, and not using any function or including the main method for execution.

You won’t loose any marks. But remember, Java is an OOP language. You should implement functions.

Sir, thanks a lot for posting a variety of practice questions on the site…..it helped me throughout last year and on the day of my ISC Practicals…!!

// waiting for your expert comments on the theory exam coming our way.

Sir, thank u for posting a variety of practice questions on the site……it helped me throughout last year and finally on the day of my ISC Practicals…!! // Sir, trust me when i say this : many computer students need a teacher like u

Will marks be deducted if we have written the datatype of variable ‘i’ as int instead of long?

No

This site is really very helpful. I owe my ability of solving java programs to this site.Because of continuous practice of programs i was able to execute my program successfully today in my isc practical exam.And the viva questions given in the notes section was also very helpul.

Thank you very much.

Sir, instead of using while as in “while(m<10000)" , I have used "for(i=m;;i++)" so that the loop may continue till infinity (even though it doesn't). And I have declared the methods to calculate sum and no. of digits as 'static' so that object doesn't need to be created.

Is there any possibility that i m gonna loose marks…???

No you won’t loose marks.

will marks be deducted if we didn’t check for no’s which don’t add up to the number in the above program?

No