Java Program to Check a Number is Palindrome or Not

In this post we will explore the basic java program to check a number is palindrome or not and explaining the concept of palindromic numbers and how the program works.

Palindrome:

A palindrome number is a number that remains the same when its digits are reversed.
For example, 121 ,12321 are palindrome numbers, but 1234 is not.

Problem Statement :

The problem is that you are given a number, and you have to check whether the number is a palindrome number or not.

Example 1:

Input1 :

121

Output 1: 

The number 121 is a Palindrome Number.

Example 2:

Input2 :

123

Output2: 

The number 321 is not a Palindrome Number.

Explanation :

For the number 121, you can see that if you read the number from backwards or forward, it reads the same. So, this number is a palindrome.

But for the number 123, it’s not a palindrome as it doesn’t read the same for the backwards and forward

Solution :

Algorithm:

  1. Take the number as an input.
  2. Store it in a temporary variable.
  3. Reverse the number.
  4. Compare the reversed number with the temporary number.
public class PalindromeNumber {
    public static void main(String[] args) {
        int number = 121;
        int temp = number;
        int reverse = 0,rem;
        while(number > 0) {
            rem = number % 10;
            reverse = reverse * 10 + rem;
            number /= 10;
        }

        if(temp == reverse) {
            System.out.printf("The Number %d is a Palindrome Number",+temp);
        }
        else{
            System.out.printf("The Number %d is not a Palindrome Number",+temp);
        }
    }
}

Ouput:

The Number 121 is a Palindrome Number

More practice programs:

Java Program to Reverse a Number

Java Program to check if a String is Palindrome or not

Java Program to Count Digits of a Number

Java Program to Print Multiplication Table of a Number

Leave a Comment

%d bloggers like this:
Python4U
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.