/**

* Java program to check if number is Palindrome.

* A palindrome number is a number that remains the same when its digits are reversed.

*/

public class Palindrome {

public static void main(String args[]){

System.out.println(“Is 121 palindrome? ” + isPalindrome(121));

System.out.println(“Is 12321 palindrome? ” + isPalindrome(12321));

System.out.println(“Is 1212 palindrome? ” + isPalindrome(1212));

//another way to check if int number is palindrome or not in java

System.out.println(“Is 121 palindrome? ” + isPalindrome2(121));

System.out.println(“Is 12321 palindrome? ” + isPalindrome2(12321));

System.out.println(“Is 1212 palindrome? ” + isPalindrome2(1212));

}

/** Java Program to check if given number is Palindrom or not */

public static boolean isPalindrome

public class PalindromeNumber {

public static void main(String[] args) {

System.out.println(“Enter the number to check if it is a palindrome”);

Scanner sc = new Scanner(System.in);

int number = sc.nextInt();

boolean flag = false;

int temp = number, reverse = 0;

while (temp != 0) {

reverse = reverse * 10 + temp % 10;

temp /= 10;

}

if (number == reverse) {

flag = true;

}

if (flag == true) {

System.out.println(“The given number is a palindrome.”);

} else {

System.out.println(“The given number is not a palindrome.”);

}

} // main method ends here

} //class ends here

A palindrome number is a number that is equal to its reverse. For example, 121 is a palindrome because it is equal to its reverse 121. Similarly, the number 565 is also a palindrome because it is equal to its reverse 565.

In this program we first take a number from user and store it in an integer variable num. Then we use the while loop to iterate until num > 0. In each iteration of while loop, we extract the last digit of user entered number by using modulus operator % (num % 10). After that we multiply the reverse variable by 10 and add last digit to reverse like:

reverse = (reverse * 10) + lastDigit;

import java.util.Scanner;

public class Palindrome{

public static void main(String[] args){

int r,sum=0,temp;

Scanner sc = new Scanner(System.in);

System.out.println(“enter value :”);

int n=sc.nextInt();

temp=n;

while(n>0){

r=n%10;

sum=(sum*10)+r;

n=n/10;

}

if(temp==sum)

System.out.println(“palindrome number “);

else

System.out.println(“not palindrome”);

}

}

import java.io.*;

class PalindromeExample{

public static void main(String args[])throws IOException{

int r,sum=0,temp;

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

System.out.println(“Enter your number”);

int n=Integer.parseInt(in.readLine()); //It is the number variable to be checked for palindrome

temp=n;

while(n>0){

r=n%10; //getting remainder

sum=(sum*10)+r;

n=n/10;

}

if(temp==sum)

System.out.println(“palindrome number “);

else

System.out.println(“not palindrome”);

}

}

Save the below program as Palindrome.java.

import java.util.*;

public class Palindrome {

public static void main(String[] args) {

int num = 121, reversedInteger = 0, remainder, originalInteger;

originalInteger = num;

// reversed integer is stored in variable

while( num != 0 )

{

remainder = num % 10;

reversedInteger = reversedInteger * 10 + remainder;

num /= 10;

}

if (originalInteger == reversedInteger) System.out.println(originalInteger + ” is a palindrome.”);

else System.out.println(originalInteger + ” is not a palindrome.”);

}

}

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward. Examples of numerical palindromes are:

23322

110011

54322345

For a given number num, write a function to test if it’s a numerical palindrome or not and return a boolean (true if it is and false if not). Return “Not valid” if the input is not an integer or less than 0.

Tomy

Tomy is a contributor at AskMeCode. We are committed to providing well-researched, accurate, and valuable content to our readers.

You May Also Like

Why <insert_coding_school_here> Is Better

Why <insert_coding_school_here> Is Better

is the best bootcamp, and you should attend. Here's why: Quality curriculum: Our curriculum is designed by industry professionals to...

Java Minor Updates and Predictions

Java Minor Updates and Predictions: A blog on Java minor updates and predictions regarding new features and changes. The JCP...

We Refactor Your Code, We’re Legends. Your Coder Coders

We Refactor Your Code, We’re Legends. Your Coder Coders

Your Coder Coders have been refactoring code since the dawn of time. Our first customer, a caveman named Oog, was...

10 Things You Can Do with Google Sheets

10 Things You Can Do with Google Sheets

Google Sheets is a spreadsheet app that lives inside of Google Drive. You can easily use it for collaborative purposes...

Leave a Reply

About | Contact | Privacy Policy | Terms of Service | Disclaimer | Cookie Policy
© 2026 AskMeCode. All rights reserved.