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

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

**So how do you check if a number is a palindrome in java?**

For example 121 is a palindrome because it reads the same forwards as well as backwards. While 173 is not a palindrome.

Because if you reverse you will read it as 371. The procedure to approach this problem would be like this,

- First we have to reverse the given number.
- Then we have to compare the given number with the actual number to check if they both match or not.
- If they both match that means the number is palindrome and if it does not match that means the number is not palindrome.

Now here’s the function “javaCheckPalindrome” that takes the number to be checked as input and inside the function we are comparing that number with the output of the function “reverse”. Here’s the code,

public static boolean javaCheckPalindrome(int number) { return (number == reverse(number)); }

“reverse” function will determine the reverse of the number and compares the output of the “reverse” function with the actual number to check if we have a palindrome or not.

Also read – best way in java to swap two numbers

So the actual logic goes inside the function “reverse”. Let’s see the code,

public static int reverse(int number) { int numReverse = 0; while(number > 0) { int a = number % 10; numReverse = numReverse * 10 + a; number /= 10; } return numReverse; }

To check reverse of a number what we have done is, first we have created a variable “numReverse” which will store the reverse number and inside while loop we have written simple logic.

First we are dividing a number by 10 and storing the remainder in integer variable “a”.

To calculate the reverse number we multiply the existing number by 10 and add the remainder that we just obtained. Then again for the next iteration we divide our original number by 10.

So let’s say we have an example of 531. So in the first iteration integer variable “a” would be 1, a reverse number would be one and when we divide the number by 10 for the next iteration we have 53.

Also read – string palindrome java

In next iteration variable “a” would be 3. The reverse number would be current reverse number (int numReverse ) which is 1 into 10 plus 3.

Which is 13. Again we divide the number 10. This time variable “a” would be 5 and we will have reverse number and 135.

This function will give you the reverse of the number which we will use to compare with the original number.

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

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:**