Form Palindrome In Java
Take two positive integer of two digits or more, reverse the digits, and add to the original number. This is the operation of the reverse-then-add-sequence. Now repeat the procedure with the sum so obtained until a palindromic number is obtained.
import java.util.*;
class Reverse_sequence
{ public static boolean isPalindrome(int n)
{ int d,r=0,p=n;
while(p!=0)
{ d=p%10;
r=r*10+d;
p=p/10;
}
if (r==n)
return true;
else
return false;
}//end of fucntion isPalindrome
public static int reverse(int n)
{ int m=n,d=0;
while(m!=0)
{ d=d*10+(m%10);
m=m/10;
}
return d;
}
public static void main()
{ int n,s;
Scanner sc= new Scanner (System.in);
System.out.println("Enter a no.");
n=sc.nextInt();
if(n<10)
System.out.println("You have give a one digit no.");
else
{ s=n;
while(true)
{s=s+reverse(s);
if(isPalindrome(s)==true)
{System.out.println(s);
break;
}
System.out.println(s);
}//end of else
}}}
OUTPUT---
Enter a no.
5280
6105
11121
23232
import java.util.*;
class Reverse_sequence
{ public static boolean isPalindrome(int n)
{ int d,r=0,p=n;
while(p!=0)
{ d=p%10;
r=r*10+d;
p=p/10;
}
if (r==n)
return true;
else
return false;
}//end of fucntion isPalindrome
public static int reverse(int n)
{ int m=n,d=0;
while(m!=0)
{ d=d*10+(m%10);
m=m/10;
}
return d;
}
public static void main()
{ int n,s;
Scanner sc= new Scanner (System.in);
System.out.println("Enter a no.");
n=sc.nextInt();
if(n<10)
System.out.println("You have give a one digit no.");
else
{ s=n;
while(true)
{s=s+reverse(s);
if(isPalindrome(s)==true)
{System.out.println(s);
break;
}
System.out.println(s);
}//end of else
}}}
OUTPUT---
Enter a no.
5280
6105
11121
23232
Comments
Post a Comment