ISC Computer Science Practical 2015 – Question 1: Introduction
In this programming problem, we are required to find the smallest integer greater than a given number M whose sum of digits is equal to another given number N. The problem is based on Question 1 of the ISC Computer Science Practical Examination 2015. It provides an opportunity to understand the application of loops, conditional statements, modulus operators, integer division and input validation in Java.
Problem Statement
Given two positive numbers M and N, such that M is between 100 and 10,000 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, the smallest integer greater than 100 whose digits add up to 11 is 119.
Write a Java program to accept M and N from the user and display the smallest required number whose sum of digits is equal to N. Also, display the total number of digits present in the required number.
The program should validate the inputs and display an appropriate message for invalid input.
Understanding the Logic
Let us consider the following input:
- M = 100
- N = 11
We need to find the smallest integer greater than 100 whose digit sum is 11.
The search starts from 101 (M + 1). Each successive number is examined by calculating the sum of its digits.
For example:
Number | Sum of Digits |
101 | 2 |
102 | 3 |
103 | 4 |
104 | 5 |
105 | 6 |
106 | 7 |
107 | 8 |
108 | 9 |
109 | 10 |
110 | 2 |
111 | 3 |
112 | 4 |
… | … |
119 | 11 |
When the program reaches 119, the sum of its digits becomes equal to N. The search terminates, and 119 is displayed as the required number.
Algorithm
- Start.
- Declare integer variables M, N, num, temp, sum and count.
- Accept the values of M and N from the user.
- Check the validity of the inputs:
- M must be between 100 and 10,000 (inclusive).
- N must be greater than 0 and less than 100.
- If either condition is not satisfied, display INVALID INPUT.
- Otherwise, initialize num with M + 1.
- Use a for loop to search for the required number.
- For every number:
- Assign num to temp.
- Initialize sum and count to 0.
- Extract the last digit using temp % 10.
- Add the extracted digit to sum.
- Increment count to calculate the number of digits.
- Remove the last digit using temp / 10.
- Repeat until temp becomes 0.
- Check whether sum is equal to N.
- If the condition is true, terminate the loop using break.
- Otherwise, increment num and continue searching.
- Display the required number and total number of digits.
- Stop.
Java Program
The following program uses a for loop for searching and a nested while loop for calculating the sum of digits and counting the number of digits simultaneously.
import java.util.Scanner;
class Question1_2015
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int M, N, num, temp, sum, count;
System.out.print(“Enter M: “);
M = sc.nextInt();
System.out.print(“Enter N: “);
N = sc.nextInt();
// Checking validity of input
if(M < 100 || M > 10000 || N <= 0 || N >= 100)
{
System.out.println(“INVALID INPUT”);
}
else
{
//num = M + 1;
// Searching for the required number
for( num = M+1; ; )
{
temp = num;
sum = 0;
count = 0;
// Calculating sum of digits
while(temp > 0)
{
int dig= temp % 10;
sum = sum + dig;
count++; //counting digits
temp = temp / 10;
}
if(sum == N)
{
break;
}
num++;
}
System.out.println(“The required number = ” + num);
System.out.println(“Total number of digits = ” + count);
}
}
}
Explanation of the Program
- Input and Variable Declaration
int M, N, num, temp, sum, count;
- M: Stores the first input number.
- N: Stores the required sum of digits.
- num: Stores the number currently being checked.
- temp: Stores a temporary copy of num for digit extraction.
- sum: Stores the sum of the digits.
- count: Counts the total number of digits.
- Input Validation
if(M < 100 || M > 10000 || N <= 0 || N >= 100)
{
System.out.println(“INVALID INPUT”);
}
This condition checks whether the entered values satisfy the restrictions given in the question.
If M is less than 100 or greater than 10,000, or N is not between 1 and 99, the program displays an invalid input message.
- Searching for the Required Number
for(num = M + 1; ; )
The for loop starts checking from M + 1 because the required number must be strictly greater than M.
The loop does not contain a termination condition in its header. It continues until the required number is found and the break statement is executed.
- Calculating the Sum of Digits
while(temp > 0)
{
int dig = temp % 10;
sum = sum + dig;
count++;
temp = temp / 10;
}
This while loop extracts the digits one by one.
Consider the number 119:
Iteration | temp | dig = temp % 10 | sum | count |
1 | 119 | 9 | 9 | 1 |
2 | 11 | 1 | 10 | 2 |
3 | 1 | 1 | 11 | 3 |
After the third iteration, temp becomes 0 and the loop terminates.
The final values are:
- Sum = 11
- Count = 3
Thus, both the sum of digits and the total number of digits are calculated in the same loop.
- Checking the Required Condition
if(sum == N)
{
break;
}
Once the calculated sum becomes equal to N, the break statement terminates the for loop.
If the condition is false, num++ increases the number by 1, and the next number is checked.
- Displaying the Result
System.out.println(“The required number = ” + num);
System.out.println(“Total number of digits = ” + count);
After finding the required number, the program displays the number and its digit count.
Sample Input and Output
Example 1
Input:
Enter M: 100
Enter N: 11
Output:
The required number = 119
Total number of digits = 3
Example 2
Input:
Enter M: 1500
Enter N: 25
Output:
The required number = 1699
Total number of digits = 4
Example 3 – Invalid Input
Input:
Enter M: 99
Enter N: 11
Output:
INVALID INPUT
Example 4 – Invalid Input
Input:
Enter M: 112
Enter N: 130
Output:
INVALID INPUT
Important Java Concepts Used
Java Concept | Application |
Scanner class | Accepting input from the user |
if-else statement | Validating input |
for loop | Searching for the required number |
while loop | Extracting and processing digits |
Modulus operator (%) | Extracting the last digit |
Integer division (/) | Removing the last digit |
break statement | Terminating the search |
Counter variable | Counting the digits |
Conclusion
This program demonstrates how Java loops and arithmetic operators can be used together to solve a number-based programming problem.
An important feature of this solution is that the sum of digits and the total number of digits are calculated simultaneously within the same while loop. This avoids the need for a separate loop to count the digits.
The use of an unconditional for loop with a break statement also makes the searching process straightforward. Since the program checks numbers sequentially, starting from M + 1, the first number satisfying the required digit-sum condition is always the smallest possible answer.