AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
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 324
programming x 136
Posted On : 2017-07-09 14:22:26.0
profile hemana mana - anyforum.in hemana mana
1240
up-rate
6
down-rate

Answers


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  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523185518076
Reply This Thread
up-rate
5
down-rate
Comments
thank you sir..but can this possible using array ?
profile Divesh - anyforum.in Divesh
33  0  0
Posted On :2017-07-11 17:14:42.0
Leave a Comment
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();
}
profile Rishi Kumar - anyforum.in Rishi Kumar
523  1855  18076
Posted On :2017-07-11 22:53:47.0
Leave a Comment



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in