Monday, 30 September 2013

Program to find whether a given number is Palindrome or not ? if not add the number to the reverse of it, and check the sum for palindrome, repeat this until we get a palindrome

// code in java
class findPalindrome
{
public static void main(String[] args)
{
int num = 165;

System.out.println(findNumber(num));
}
static int findNumber(int num)
{
if(isPalindrome(num))
{
return num;
}
else
{
return findNumber(num + reverseNumber(num));
}
}
static int reverseNumber(int num)
{
int newNum = 0;
while(num > 0)
{
newNum = newNum*10 + num % 10;
num /= 10;
}
return newNum;
}
static boolean isPalindrome(int num)
{
return num == reverseNumber(num);
}
}

No comments:

Post a Comment