Java Program to Check if number is PALINDROME or Not
In this tutorial we will write a Java program to check if a number is a PALINDROME or not. A palindrome number(or string) is a special number which if written in reverse order also is the same. –
E.g – 121 -> reverse -> 121
1221 -> reverse -> 1221
1123211 -> reverse -> 1123211
We will be using While Loop to reverse the number first and then we simply will use the If-Else statement to check if the original number matches the reversed one.
Java program to Find if a Number is PALINDROME or not –
//Q1) WAP to check whether a integer is palindrome or not package javaapplication4; public class JavaApplication4 { public static void main(String[] args) { int num=121; int temp=num; int rev = 0; while(num>0) { rev = rev*10; rev = rev + num%10; num = num/10; } if(temp == rev) { System.out.println("Palindrome"); } else { System.out.println("Not Palindrome"); } } }
Output
Palindrome