on Leave a Comment

Java Program to Check Whether a Number is Palindrome or Not

This Java program checks whether a number is palindrome or not. A palindrome number is a number that is same after reverse.

Step to check whether a number is palindrome or not

- Get the number to check whether it is palindrome or not.
- Store that number in temporary variable.
- Reverse the number.
- Compare reversed number with temporary number.
- If both are same then print number is palindrome
- Else print number is not palindrome.

Java Program to Check Palindrome Number using for loop


class CheckPalindrome
{
 public static void main(String[] args)
 {
  int n = 151, rev = 0, rem;
  int orig = n;
                 for( ; n != 0; n = n/10)
  {
   rem = n % 10;
                        rev = rev * 10 + rem;
  } 
  if(orig == rev)
   System.out.println(orig+" is palindrome number");
  else
   System.out.println(orig+"is not palindrome number");
 }
}

Output:


151 is palindrome number


Java Program to Check Palindrome Number using while loop


class CheckPalindrome
{
 public static void main(String[] args)
 {
  int n = 282, rev = 0, rem;
  int orig = n;
                while(n != 0)
  {
   rem = n % 10;
                        rev = rev * 10 + rem;
   n = n/10;
  } 
  if(orig == rev)
   System.out.println(orig+" is palindrome number");
  else
   System.out.println(orig+"is not palindrome number");
 }
}

Output:


282 is palindrome number

0 comments:

Post a Comment