Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ISC COMPUTER SCIENCE PRACTICAL 2015 Question 1

Monday, March 11, 2019

ISC COMPUTER SCIENCE PRACTICAL 2015 Question 1


Program 1: 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 wose 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

JAVA SOURCE CODE


public class ISC2015Q1 {
       public static void main(String[] args) {
             int M,N;
             Scanner in = new Scanner(System.in);
             while(true) {
                    System.out.println("Enter the value of M:");
                    M=in.nextInt();
                    System.out.println("Enter the value of N:");
                    N=in.nextInt();
                    if( M < 100 || M > 10000 || N >= 100)  {
                          System.out.println("INVALID INPUT:");
                          continue;
                    }
                   
                    else
                          break;

                    }
             in.close();
             SumOfDigitsToN obj = new SumOfDigitsToN();
             System.out.println("The required number ="+obj.SumOfDigitsCalculation(M,N));
             System.out.println("Total number of digits="+obj.TotalNumberOfDigits());
             }
}

class SumOfDigitsToN {
       public int SumOfDigitsCalculation (int M,int N) {
             int sum,tmp,rem,k;
             do {
                    tmp=M;
                    sum=0;
                    k=0;
                    while(tmp!=0) {
                          rem = tmp%10;
                          sum +=rem;
                          tmp /=10;
                          k++;
                          }
                    M++;
             }while(sum!=N);
             numb = M-1;
             total_no_digits = k;
             return (numb);
       }

       public int TotalNumberOfDigits() {
             return (total_no_digits);
       }
       private int total_no_digits;
       private int numb;
}

No comments:

Post a Comment