AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
3. Our Experts are looking for yours ?.

# corejava-programming: Java program to check given number is Lychrel Number or not

 If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindromes so quickly. For example, 349 + 943 = 1292, 1292 + 2921 = 4213 4213 + 3124 = 7337 That is, 349 took three iterations to arrive at a palindrome. Although some numbers like 196, never produce a palindrome. A number that never forms a palindrome through the reverse and add process is called a Lychrel number. Write a java program to take a number and check whether it is a Lychrel number or not. If a number is not Lychrel then print the number of iterations that it took to produce a palindrome. If the number is Lychrel then print the 5th iteration NOTE: Check only till 10 iterations. If till 10 iterations you cannot find the palindrome then consider that number as Lychrel number. Use long datatype Sample Input: 47 Sample Output: 1 Sample Input: 349 Sample Output: 3 Sample Input: 196 Sample Output: 196 is a Lychrel Number 5th iteration of number 196 is 52514 corejava x 353programming x 169 Posted On : 2017-07-09 14:22:26.0 hemana mana 1240 6 import java.util.Scanner; public class LychrelNumber { public static void main(String[] args) { Scanner input=new Scanner(System.in); long number=input.nextLong(); checkLychrel(number); } public static void checkLychrel(long input){ boolean result=true; String fifthIterationResult=null; Long temp=input; for(int i=1;i<=10;i++){ temp=reverseAndAdd(temp); if(temp==Integer.parseInt(new StringBuilder(temp.toString()).reverse().toString())){ result=false; System.out.println(i); break; }else if(i==5){ fifthIterationResult="5th iteration of number "+input+" is "+temp; } } if(result){ System.out.println(input+" is a Lychrel Number"); System.out.print(null!=fifthIterationResult?fifthIterationResult:""); } } private static Long reverseAndAdd(Long number){ return number+Long.parseLong(new StringBuilder(number.toString()).reverse().toString()); } } Posted On : 2017-07-09 23:01:33 Satisfied : 4 Yes  2 No Rishi Kumar 523188237102Reply This Thread 5 thank you sir..but can this possible using array ?
Posted On :2017-07-11 17:14:42.0
Hi Divesh,
I think we can use array to get the reverse of a String, else if you want to store each iteration result in array, we can do that also, but i think it is not a good idea to consume more memory. Please check below code snippet

//if(temp==Integer.parseInt(getReverse(temp.toString()))){

//return number+Long.parseLong(getReverse(number.toString()));

public static String getReverse(String input){
char[] charArray=input.toCharArray();
StringBuilder sb=new StringBuilder();
for(int i=charArray.length-1;i>=0;i--){
sb=sb.append(input.charAt(i));
}
return sb.toString();
}
Posted On :2017-07-11 22:53:47.0