Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * The program inputs two integers 'm' and 'n' and prints the smallest integer greater than 'm' whose sum of digits is equal to 'n'.
- */
- import java.util.Scanner;
- public class Prac_2015
- {
- public static int findsum(int num)
- {
- int i,x;
- String a=Integer.toString(num);
- int sum=0;
- for(i=0;i<a.length();i++)
- {
- x=Character.getNumericValue(a.charAt(i));
- sum+=x;
- }
- return sum;
- }
- public static int digPresent(int num)
- {
- String a=Integer.toString(num);
- int i; int ct=0;
- for(i=0;i<a.length();i++)
- {
- ct++;
- }
- return ct;
- }
- public static void main(String args[])
- {
- Scanner sc=new Scanner(System.in);
- int m,n; //ATQ- m is between 100 and 10000 and n is less than 100.
- System.out.println("Please enter the greater number. It must be between 100 and 10000.");
- m=sc.nextInt();
- System.out.println("Please enter the smaller number. It must be a positive number lesser than 100.");
- n=sc.nextInt();
- if(m<100 || m>10000 || n<1 || n>99)
- {
- System.out.println("Invalid input. Please try again.");
- }
- else
- {
- int i; int sum=0,totaldig;
- i=m+1;
- while(i>m)
- {
- sum=findsum(i);
- if(sum==n)
- {
- break;
- }
- i++;
- }
- totaldig=digPresent(i);
- System.out.println("The required number = "+i);
- System.out.println("Total number of digits = "+totaldig);
- }
- sc.close();
- }
- }
Add Comment
Please, Sign In to add comment