Menu
Topics Index
...
`

Find if a number is palindrome


If you need explanation Read this topic

If you need Answer Take test on this topic

Write a program to find if the given number is palindrome. A palindrome number which when reversed will be the same as original.

Input (Integer) Output (Boolean)
121 true
123 false
7887 true
90009 true
90010 false



class PalindromeNumber
{
    public static void main(String s[])
    {
        boolean palindrome = isPalindrome(1221);
        
        if(palindrome)
            System.out.println("1221 is a palindrome.");
        else
            System.out.println("1221 is not a palindrome.");

    }


    public static boolean isPalindrome(int number)
    {
        boolean result = false;
        return result;
    }

}
Topic: while Loop In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

int temp = number;
int num = 0;
while(temp!=0)
{
int t = temp % 10;
num = (num * 10) +  t;
temp = temp / 10;
}
if(num == number)
    result = true;
else
        result = false;

Posted by Gopinath Manchikanti    2014-11-26 05:49:43


int palindrome = number;  
int reverse = 0;
while (palindrome != 0) {
int remainder = palindrome % 10;
reverse = reverse * 10 + remainder;
palindrome = palindrome / 10;
}
if (number == reverse)
  result=true;

Posted by Pooja Singh    2014-11-26 06:08:56


Algorithm for palindrome
1.Input a number
2.Take 2 variables one reverse intialised to 0 and other palindrome
3.perform modulus % with 10 for the input number
4.we are left with remainder.
5.The remainder contains the right most digit and perform rem=rem*10+rem;
6.divide the input by 10 which gives quotient
7.iterate untill we get left most digit.
8. if reverse number =input
    its palindrome
   else
     its not palindrome

Posted by Sai Veerendra    2014-11-26 08:41:44


Here we the programmer should aware of palindrome. The palindrome number which when reversed will be the same as original. so here we initialize two variables one is palindrome element and other is reverse which is initialized to 0. Here we can use while for performing palindrome. Palindrome!=0 is the expression used in while , in the next step, initializing and declaring the input number and  can perform module % by 10.

Later, reverse can be multiplied with 10 and added to remainder and stores the result in reverse(reverse=reverse*10+remainder)
Later, the palindrome number can be divided by 10 and result stored in palindrome. finally if reverse equal to the number then it is palindrome says its true, else Its false.

Posted by Sai Ram    2014-11-26 18:56:13


Please do not include the code directly only provide a hint which will make the users think and try the program using the given approach.

Posted by Merit Campus    2014-11-27 06:07:16


This dose is now closed and the winners are Sai Veerendra,  for 'First Correct Comment', Sai Ram,  for 'Second Correct Comment'. The 'lucky liker' is Sai Ram. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-27 07:41:52


© meritcampus 2019

All Rights Reserved.

Open In App