**Palindrome number program in java** is to check whether the number is palindrome or not.

In this tutorial we are going to see what is palindrome number, How to check that number is palindrome number, and palindrome number in java.

**What is Palindrome number ?**

**Palindrome meaning : **Any number which remains same when we reverse it.

for example : **121**, **212**, **12321 **

as we see if we reverse these numbers still all numbers remains same.

**Logic to check Palindrome number in java :**

For this first we take a number from user. we reverse the given number. after that we compare both given and reversed number if both are equal then number is palindrome number otherwise not.

**Example :**

number = 121

reverse number = 121

Number is palindrome.

**Algorithm :**

- Start
- Declare variables
- Take number from user
- Reverse the given number
- Compare both numbers
- if both equal then print palindrome number
- End

## Palindrome number in java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.*; public class Main { public static void main(String[] args) { int n, reverse = 0, t; Scanner sc=new Scanner(System.in); System.out.println("Enter a number to check if it is a palindrome or not"); n=sc.nextInt(); t = n; while (t != 0) { reverse = reverse * 10; reverse = reverse + t%10; t = t/10; } if (n == reverse) System.out.println(n+" is a palindrome number."); else System.out.println(n+" isn't a palindrome number."); } } |

**Output :**

**Explanation :**

**1. Declare the variables **

**n, reverse = 0, t; n => to store number user enters,**

** reevverse => to store reverse of given number, t is temporary variable.**

**2. Next we copy the given number into temporary variable. **

t = n;

**3. Now we perform the reverse of the given number mathematically. for this we use while loop. **

while (t != 0) which is used to traverse the number till end.

**Now the main logic comes here**

let the number ‘n’ be 321 and as 321>0, while loop gets executed

then x=321%10—>which is 1.

rev=0*10+1——–>1

n=321/10———>32

The rev for the first loop execution is rev=1.

**Now the number ‘n’ has become ’32’ and n>0, while loop executes for the 2nd time.**

then x=32%10—>which is 2.

rev=1*10+2——–>12

n=32/10———>3

The rev when loop executed the second time is rev=12.

**Now the number ‘n’ has become ‘3’ and n>0, while loop executes for the 3rd time**

then x=3%10—>which is 3.

rev=12*10+3——–>123

n=3/10———>0

The rev when loop executed the third time is rev=123.

**4. Now as the number of variable ‘n’ is 0 which is not n>0 then the loop terminates.Then the final reverse is ‘123’.**

**5. Now compare reverse and given number.**

**6. If both Re equal pint number is palindrome else not.**

## Leave a Reply