Write a program which takes a four-digit number as an argument and find out in how many iterations 6174 is reached for input number.

 

Kaprekar discovered the Kaprekar constant known as 6174. He showed that 6174 is reached in the limit as one repeatedly subtracts the highest and lowest numbers that can be constructed from a set of four digits that are not all identical. 

for example, for number 1234 we have

Iteration-1 : 4321 - 1234 = 3087

Iteration-2 : 8730 - 0378 = 8352 Iteration-3 : 8532 - 2358 = 6174.

So, process completes in 3 iteration.

The number 6174 is reached in N iteration where N is different for different 4 digits numbers.

Write a program which takes a four-digit number as an argument and find out in how many iterations 6174 is reached for input number.

 

-  If user does not provide input number then display: "Missing required inputs"

-  If user does not provide valid four-digit number then display: "Invalid number"

-  If user provides a number with all identical digits then display: "All digits are identical so 6174 cannot be reached"

-  If number does not converge to 6174 in at most 7 iteration then display "6174 cannot be reached" 

Input

Command Line Argument: <<Four-digit number>> 

Output

[6174 is reached in N iterations] or [All digits are identical so 6174 cannot be reached] or [6174 cannot be reached] or [Missing required inputs] or [Invalid number]  

Example

Example 1:

            Inputs: Command Line Argument: 1234       

            Output: 6174 is reached in 3 iterations

Example 2:

            Inputs: Command Line Argument: 2222       

            Output: All digits are identical so 6174 cannot be reached

solution

import java.io.*;
import java.util.*;

public class Main
{
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt(); 
for(int p=0;p<t;p++)
{
int num=sc.nextInt();   String s=new String();
boolean flag=false;     int iter=0boolean mark=true;
    s=num+"";   char b[]=s.toCharArray();   Arrays.sort(b);
    s=new String(b);
    while(s.length()<4)
    s="0"+s;
    /*if(s.charAt(0)==s.charAt(1)&&s.charAt(1)==s.charAt(2)&&s.charAt(0)==s.charAt(2))
    mark=false;
    if(s.charAt(3)==s.charAt(1)&&s.charAt(1)==s.charAt(2)&&s.charAt(3)==s.charAt(2))
    mark=false;*/
    
    while(mark)
    {
    s=num+"";
        while(s.length()<4)
        s="0"+s;
    char a[]=s.toCharArray();   Arrays.sort(a);
        //if((a[0]==a[1]&&a[1]==a[2]&&a[0]==a[2])||(a[1]==a[2]&&a[2]==a[3]&&a[1]==a[3]))
        //break;
    String s1=new String();
        for(int i=3;i>=0;i--)
        s1+=a[i]+"";
    s=String.copyValueOf(a);
    //System.out.println(s1+" "+s);
    num=Integer.parseInt(s1)-Integer.parseInt(s);
    iter++;
        if(num==0)
        break;
        else
        if(num==6174)
        {
        flag=true;
        break;
        }
    }
if(flag==true)
System.out.println("6174 is reached in " + iter + " iterations" );
else
System.out.println("All digits are identical so 6174 cannot be reached");
}
}
}

/* 
Output : 
1                                                                                                                             
1234                                                                                                                          
6174 is reached in 3 iterations  

*/

No comments:

Get new posts by email:


APY Logo