class Palindrome{ public static void main(String args[]){ long Number=0; int count=0; try{// if the user does not enter a number as an argument, catch the error and let the user know what the error is. //it is better because the error generated by the compiler could be hard to understand by the users who do not know about the code Number=Long.parseLong(args[0]); }catch(Exception e){ System.out.println("Please give a number as argument"); System.exit(0); } // loop till number becomes a palindrome while(!IsPalindrome(Number)){ System.out.print(Number+"+"+Reverse(Number));//print the number and reverse of it Number=Add2Nums(Number,Reverse(Number));//add the number with reverse of it and assign it to the same variable System.out.println("="+Number); //print the sum of the two numbers count++; // count the number of times we have added } System.out.println(Number+" is a palindrome"); System.out.println("No of Steps = "+count); } //Reverses a number public static long Reverse(long number){ long temp=0,radix=0; while(number>0){ radix=number%10; number/=10; temp=temp*10 + radix; } return temp; } //checks if the number is a palindrome or not public static boolean IsPalindrome(long number){ if(number==Reverse(number)) return true; else return false; } //adds two numbers public static long Add2Nums(long number1,long number2){ return number1+number2; } }