Palindrome Number In Java

Sharing is healthy !!

Hey guys!! , hello, how are you doing…. Welcome to flower brackets blog. In this post we are going to learn palindrome number in java.

Palindrome Number

Palindrome number is a number where number remain the same when its numbers are reversed.

example: Palindrome Number In Java

Also Read – Armstrong Number In Java Program

import java.util.Scanner;

public class PalindromeNumberInJava
{
   public static void main(String[] args)
   {
      System.out.println("Please enter a number to reverse : ");
      Scanner sc = new Scanner(System.in);
      int number = sc.nextInt();
      int x = number;

      // Here number entered is reversed
      int reverse = 0;
      int y;

      while(number > 0)
      {
         y = number % 10;
         reverse = reverse * 10 + y;
         number = number / 10;
      }

      if(reverse == x)
      {
         System.out.println(x + " is a Palindrome Number.");
      }
      else
      {
         System.out.println(x + " is not a Palindrome Number.");
      }
   }
}

Output:

Palindrome Number In Java

Now, let us understand how above example executes. First a number is received as an input and stored in temporary variable(int x).

In the next step we will reverse the number and then compared with the reversed number.

Finally both numbers are checked, if both numbers are same then it is “palindrome number” else “it is not a palindrome number”.

Sharing is healthy !!

You May Also Like