Palindrome number java

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

palindrome number java

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

For example : 141, 101, 181, 12321 etc..

Also read – best way to swap two numbers in java

Palindrome algorithm

Here first user enters a number. This number is stored in temporary variable. Then number is reversed. Next compare temporary number with reversed number.

Also read – string palindrome java

If both temporary number and reversed number are same then it is a palindrome number else it’s not a palindrome number.

Now let’s see java program to check whether a number is palindrome or not,

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 reverse, total = 0, temp;
      int number = sc.nextInt();
      temp = number; 
      while(number > 0)
      { 
         reverse = number % 10; 
         total = (total * 10) + reverse; 
         number = number / 10;
      } 
      if(temp == total)
      {
         System.out.println(total + " is a Palindrome Number");
      }
      else
      {
         System.out.println(total + " not a palindrome number");
      }
      sc.close();
   }
}

Output:

Please enter a number to reverse : 101
101 is a Palindrome Number

Related Posts