# Number Chains Algorithm 1. Get a number (M) as an integer number 2.

Check M value If M is a positive number greater than 0 then do next step , if M=0 then terminate the program , otherwise just re input M 3. Set N by get a descending value of M 4. Set O by an ascending value of M 5. Set P=N – O 6. Set counter=1 7. Print M as original number 8. Do step 9 until 13 while N – O is not same P ( I use Do … While ) , otherwise jump to step 14 9. Print number chain ( N – O = P ) 10. Set P =N – O 11. Set N by get a descending value of P 12. Set O by get an ascending value of P 13. Increase the counter by 1 14. Print the counter as chain length 15. Back to step 1 import java.util.Scanner; class NumberChains { String getAsc(int num){ char temp; char no[]=String.valueOf(num).toCharArray(); for (int i =0 ; i < (no.length)-1; i++) { for (int j =0 ; j < (no.length)-1-i; j++) { if(no[j]>no[j+1]){ temp=no[j]; no[j]=no[j+1]; no[j+1]=temp; } } } return String.valueOf(no, 0, no.length); } String getDesc(int num){ char temp; char no[]=String.valueOf(num).toCharArray(); for (int i =0 ; i < (no.length)-1; i++) { for (int j =0 ; j < (no.length)-1-i; j++) { if(no[j]<no[j+1]){ temp=no[j]; no[j]=no[j+1]; no[j+1]=temp; } } } return String.valueOf(no, 0, no.length); }

if(M!=0){ int N=Integer.parseInt(nc.out. N=Integer. } } } } .getDesc(M)). do{ System.nextInt().parseInt(nc. counter=1. O=Integer. System. int M=-1.getDesc(P))."+O+" = "+(N-O)).println("Chain length "+counter). NumberChains nc=new NumberChains(). counter++.println("Original number was "+M).parseInt(nc. }while(N-O!=P).getAsc(P)).getAsc(M)). while(M!=0){ M=s.out. int P=N-O.println(N+" . int O=Integer.parseInt(nc.out. int counter. System.public static void main(String[] args) { Scanner s=new Scanner(System.in). P=N-O.