Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

java If we input a number formed by 4 digits and these digits are not all of the

ID: 3880436 • Letter: J

Question

java

If we input a number formed by 4 digits and these digits are not all of the same value, then it obeys the following law. Let us process the number in the following way:

Arrange the digits in the way from bigger to smaller, such that it forms the biggest number that could be made from these 4 digits;

Arrange the digits in the way from smaller to bigger, such that it forms the smallest number that could be made from these 4 digits (If there is 0 among these 4 digits, the number obtained may be less than four digits);

Find the difference of these two numbers that is a new four digital number.

Repeat the above process (1-3), we get the result 6174 or 0.

Write the program to implement the above algorithm.

The output from the program should show the procedure for finding this number and the number of repetitions.

Sample Input

Sample Output

5364

2221

4444

-1

N=5364:

6543-3456=3087

8730-378=8352

8532-2358=6174

Ok!! 3 times

N=2221:

2221-1222=999

999-999=0

Ok!! 2 times

N=4444:

No!!

Sample Input

Sample Output

5364

2221

4444

-1

N=5364:

6543-3456=3087

8730-378=8352

8532-2358=6174

Ok!! 3 times

N=2221:

2221-1222=999

999-999=0

Ok!! 2 times

N=4444:

No!!

Explanation / Answer

import java.util.*;

public class program{

public static void main(String[] args){

Scanner scan= new Scanner(System.in);

int a= scan.nextInt();

int diff=1,p=0;

boolean check=true;

check = check_number(a);

if(check){

//loops until the difference is either 0 or 6174

while(diff!=0&&diff!=6174){

int c=greatest(a);

int d=lowest(a);

diff=c-d;

System.out.println(c+" - "+d+" = "+diff);

p++;

a=diff;

}

System.out.println("OK!! "+p+" times");

}

else

System.out.println("NO!!");

}

//method to check whether the digits of the number are all not equal

private static boolean check_number(int a) {

int ar[]=getarray(a),p=0;

for(int i=0;i<ar.length-1;i++){

if(ar[i]==ar[i+1])

p++;

}

return (p!=ar.length-1);

}

//number of digits in the number is calulated so as to intialize the size of the array

private static int size_of_number(int a) {

int k=0;

while(a!=0){

a/=10;

k++;

}

return k;

}

//method to give largest number possible with given number

private static int greatest(int a) {

int ar[]=getarray(a);

//array is sorted using bubble sort in decreasing order

for(int i=ar.length-1;i>=0;i--)

for(int j=0;j<i;j++)

{

if(ar[j]<ar[j+1]){

int t=ar[j+1];

ar[j+1]=ar[j];

ar[j]=t;

}

}

//sum of the digits in the array is calculated to get the greatest number

int sum=0;

for(int i=0;i<ar.length;i++){

sum=sum*10+ar[i];

}

return sum;

}

//method to give smallest number possible with given number

private static int lowest(int a) {

int ar[]=getarray(a);

//array is sorted using bubble sort in increasing order

for(int i=ar.length-1;i>=0;i--)

for(int j=0;j<i;j++)

{

if(ar[j]>ar[j+1]){

int t=ar[j+1];

ar[j+1]=ar[j];

ar[j]=t;

}

}

//sum of the digits in the array is calculated to get the lowest number

int sum=0;

for(int i=0;i<ar.length;i++){

sum=sum*10+ar[i];

}

return sum;

}

//method to return array of the digits of given number

private static int[] getarray(int a){

int ar[]=new int[size_of_number(a)];

int k=0;

while(a!=0){

ar[k]=a%10;

a/=10;k++;

}

return ar;

}

}