# Check Palindrome in Java Program

JavaObject Oriented ProgrammingProgramming

In this article, we will understand how to check if the given number is a Palindrome number. A palindrome is a word, number, phrase, or other sequences of characters which reads the same backward as forward. Word such as malayalam or the number 10101 are a palindrome.

For a given string if reversing the string gives the same string then we can say that the given string is a palindrome. Which means to check for the palindrome, we need to find whether the first and last, second and last-1, and so on elements are equal or not.

Below is a demonstration of the same −

Input

Suppose our input is −

Enter the number : 454

Output

The desired output would be −

The number 454 is palindrome number


## Algorithm

Step 1 - START
Step 2 - Declare four integer values namely my_input, my_reverse, my_sum and my_temp
Step 3 - Read the required values from the user/ define the values
Step 4 - Using a while loop, compute the reverse of the input value using ‘ * % /’ opeartors
Step 5 - Compute my_temp%10 and assign it to my_reverse.
Step 6 - Compute (my_sum * 10) + my_reverse and assign it to my_sum
Step 7 - Compute my_temp / 10 and assign it to my_temp.
Step 8 - Repeat the steps and check if the input value is equal to my_sum value. If yes, it’s a palindrome number, else it’s not a palindrome number.
Step 9 - Display the result
Step 10 - Stop

## Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in our coding ground tool .

import java.util.Scanner;
public class Palindrome {
public static void main(String args[]){
int my_input, my_reverse, my_sum, my_temp;
System.out.println("Required packages have been imported");
Scanner my_scanner = new Scanner(System.in);
System.out.println("A reader object has been defined ");
System.out.print("Enter the number : ");
my_input = my_scanner.nextInt();
my_sum = 0;
my_temp = my_input;
while ( my_temp > 0 ){
my_reverse = my_temp%10;
my_sum = (my_sum * 10) + my_reverse;
my_temp = my_temp / 10;
}
if( my_input == my_sum )
System.out.println("The number " +my_input +" is palindrome number ");
else
System.out.println("The number " +my_input +" is not palindrome number ");
}
}

## Output

Required packages have been imported
A reader object has been defined
Enter the number : 454
The number 454 is palindrome number

## Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class Palindrome {
public static void main(String args[]){
int my_input, my_reverse, my_sum, my_temp;
my_input = 454;
System.out.println("The number is defined as " +my_input);
my_sum = 0;
my_temp = my_input;
while ( my_temp > 0 ){
my_reverse = my_temp%10;
my_sum = (my_sum * 10) + my_reverse;
my_temp = my_temp / 10;
}
if( my_input == my_sum )
System.out.println("The number " +my_input +" is palindrome number ");
else
System.out.println("The number " +my_input +" is not palindrome number ");
}
}

## Output

The number is defined as 454
The number 454 is palindrome number
Updated on 22-Feb-2022 11:56:34