• Register

Introduction to Palindrome in Java Baeldung

A string or number is said to be a palindrome if it remains the same even after it has been reversed. For example, "MADAM" is a palindrome string as it is spelled "MADAM" even if it is reversed. But in the case of "LUCKY" this string is not a palindrome, since it is "YKCUL" when it is reversed. Some of the numbers palindrome - 365563, 48984, 12321, 171, 88, 90009, 343, and some lines palindrome - a MADAM, MALAYALAM, LOL, DAD, MOM, C ++ & ++ C and t . D. Let's look at the logic and the implementation of a palindrome in the following sections. In this thread, we are going to learn about Palindrome in Java.

Palindrome logic in Java

To check if a number is a palindrome, the following algorithm can be used.

  • Take an input string or number that you want to check if it is a palindrome or not.

For example, let's take the number 353 as input.

  • Take the input number and copy it to a temporary variable

353-> temp

  • Cancel it using for, while or any other method of your choice.

Reversednumber: rev=353

  • Compare the entered number and the return number.

If they are the same, then the number is called the palindrome number.

Otherwise, the number is not a palindrome number.

those.

If(inputnum==rev)
( then palindrome )
Else not palindrome

How to check a palindrome using different methods?

There are several ways to check if a given input number is a palindrome or not.

  1. For loop
  2. While the loop
  3. Library method (for strings)

Let's look at each of them in detail.

1. The program for checking the palindrome number using the for loop

//Java program to check whether a String is a Palindrome or not using For Loop
import java.util.*;
public class PalindromeNumberExample (
//main method
public static void main(String() args) (
int r=0 ; //reversed Integer
int rem, num; //remainder and original number
Scanner s = new Scanner(System.in);
System.out.print("Enter number that has to be checked:");
num = s.nextInt();
//Store the number in a temporary variable
int temp = num;
//loop to find the reverse of a number
for( ;num != 0; num /= 10 )
(
rem = num % 10; // find the modulus of the number when divided by 10
r = r * 10 + rem;
)
//check whether the original and reversed numbers are equal
if (temp == r)
(
System.out.println(temp + " is input number");
System.out.println(r + " is the reversed number");
System.out.println("Since they are equal " + temp + " is a palindrome number");
)
else
(
System.out.println(temp + " is input number");
System.out.println(r + " is the reversed number");
System.out.println("Since they are not equal " + temp + " is not a palindrome number");
)
)
)

Output example 1:

image

Here, as 353 is the same when accessed, it is treated as a palindrome.

Output example 2:

image

Here, since 234 remains not the same in reverse order, it is not considered a palindrome.

2. Program for checking palindrome number using a loop

//Java program to check whether a number is a Palindrome or not using While Loop
import java.util.*;
public class PalindromeNumberExample (
public static void main(String() args) (
int r=0, rem, num;
Scanner s = new Scanner(System.in);
System.out.print("Enter number that has to be checked:");
num = s.nextInt();
//Store the number in a temporary variable
int temp = num;
//loop to find the reverse of a number
while( num != 0 )
(
rem= num % 10;
r= r * 10 + rem;
num=num/10;
)
//check whether the original and reversed numbers are equal
if (temp == r)
(
System.out.println(temp + " is input number");
System.out.println(r + " is the reversed number");
System.out.println("Since they are equal " + temp + " is a palindrome number");
)
else
(
System.out.println(temp + " is input number");
System.out.println(r + " is the reversed number");
System.out.println("Since they are not equal " + temp + " is not a palindrome number");
)
)
)

 

97,180 points
11 6 5