To Check Palindrome Number In Java

Yuvaraj
5 min readAug 8, 2024

--

Basic Java Program

A palindrome number is a number that remains the same when its digits are reversed. In other words, it reads the same forward and backward.

Characteristics:

  • Symmetry: The number is symmetrical; it looks the same when read from either direction.
  • Digit Arrangement: The arrangement of digits is such that reversing the order does not change the number.

Examples:

  1. 121:
  • Forward: 121
  • Backward: 121
  • Since both are the same, 121 is a palindrome number.

2. 12321:

  • Forward: 12321
  • Backward: 12321
  • Since both are the same, 12321 is a palindrome number.

3. 123:

  • Forward: 123
  • Backward: 321
  • Since they are different, 123 is not a palindrome number.

Note:

  • Palindromes can be of any length, including single-digit numbers, which are always palindromes because they read the same forward and backward.
  • Palindromes are not limited to numbers; words and phrases can also be palindromes if they read the same backward as forward (e.g., “radar” or “level”).
import java.util.Scanner;

public class PalindromeNumber {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter any number: ");
int number = scanner.nextInt();

if (isPalindrome(number)) {
System.out.println(number + " is a palindrome.");
} else {
System.out.println(number + " is not a palindrome.");
}
}

// Method to check if a number is a palindrome
public static boolean isPalindrome(int num) {
int originalNumber = num; // Store the original number
int reversedNumber = 0; // To hold the reversed number

// Reverse the number
while (num != 0) {
int digit = num % 10; // Extract the last digit
reversedNumber = reversedNumber * 10 + digit; // Append the digit to the reversed number
num = num / 10; // Remove the last digit from the number
}

// Check if the reversed number is equal to the original number
return originalNumber == reversedNumber;
}
}

// Output:
/* Enter any number:
5445
5445 is a palindrome. */

Explanation:

Define isPalindrome Method:

public static boolean isPalindrome(int num) {
int originalNumber = num; // Store the original number
int reversedNumber = 0; // To hold the reversed number

// Reverse the number
while (num != 0) {
int digit = num % 10; // Extract the last digit
reversedNumber = reversedNumber * 10 + digit; // Append the digit to the reversed number
num = num / 10; // Remove the last digit from the number
}

// Check if the reversed number is equal to the original number
return originalNumber == reversedNumber;
}
  • Purpose: This method checks if a number is a palindrome.
  • originalNumber: Stores the original value of num for comparison later.
  • reversedNumber: Used to build the reversed number from its digits.
  • While Loop: Extracts each digit from num and builds the reversedNumber by adding each digit to the end of it.
  • Return Statement: Compares the reversedNumber with originalNumber and returns true if they are the same, otherwise false.
while (num != 0) {
int digit = num % 10; // Extract the last digit
reversedNumber = reversedNumber * 10 + digit; // Append the digit to the reversed number
num = num / 10; // Remove the last digit from the number
}

Extract the Last Digit:

int digit = num % 10;

  • num % 10 gives you the last digit of the number.
  • Example: If num is 123, then 123 % 10 equals 3. So, digit will be 3

Build the Reversed Number:

reversedNumber = reversedNumber * 10 + digit;

  • This line adds the digit you just extracted to the reversedNumber.
  • First, multiply reversedNumber by 10 to shift its digits to the left, making space for the new digit.
  • Then, add the new digit to the shifted reversedNumber.

Example:

  • If reversedNumber is initially 0 and the first digit is 3, then:
  • 0 * 10 + 3 makes reversedNumber become 3.
  • On the next loop, if the new digit is 2 and reversedNumber is 3, then:
  • 3 * 10 + 2 makes reversedNumber become 32.

Remove the Last Digit:

num = num / 10;

  • num / 10 removes the last digit from the number.
  • This is done by dividing num by 10.
  • Example: If num is 123, then 123 / 10 equals 12. Now num is 12, and the last digit (3) is removed.

How It Works Together

For an example using the number 123:

  1. Initial State:
  • num = 123
  • reversedNumber = 0

2. First Iteration:

  • Extract Last Digit: digit = 123 % 10digit = 3
  • Build Reversed Number: reversedNumber = 0 * 10 + 3reversedNumber = 3
  • Remove Last Digit: num = 123 / 10num = 12

3. Second Iteration:

  • Extract Last Digit: digit = 12 % 10digit = 2
  • Build Reversed Number: reversedNumber = 3 * 10 + 2reversedNumber = 32
  • Remove Last Digit: num = 12 / 10num = 1

4. Third Iteration:

  • Extract Last Digit: digit = 1 % 10digit = 1
  • Build Reversed Number: reversedNumber = 32 * 10 + 1reversedNumber = 321
  • Remove Last Digit: num = 1 / 10num = 0

Now, num is 0, so the loop stops. The reversedNumber is 321, which is the reverse of 123.

Summary of the Loop:

  • Extract the last digit of the number.
  • Add that digit to the reversed number, which you’re building.
  • Remove the last digit from the original number.
  • Repeat these steps until there’s nothing left in the original number (num becomes 0).

This process turns the original number into its reversed form.

In Short:

  • It takes the original number.
  • It builds the reversed number by pulling off each digit and putting it together in reverse.
  • It then checks if the reversed number is the same as the original number.
  • If they match, it’s a palindrome. If not, it’s not.

This method does exactly this but using some basic math to reverse the digits.

Using String Manipulation

Here’s the simplified approach:

  1. Convert the number to a string.
  2. Reverse the string.
  3. Check if the original string is the same as the reversed string.
import java.util.Scanner;

public class PalindromeNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter any number: ");

int number = sc.nextInt();

if (isPalindrome(number)) {
System.out.println(number + " is a palindrome.");
} else {
System.out.println(number + " is not a palindrome.");
}
}

public static boolean isPalindrome(int num) {
// Convert the number to a string
String original = Integer.toString(num);

// Reverse the string
String reversed = new StringBuilder(original).reverse().toString();

// Compare the original string with the reversed string
return original.equals(reversed);
}
}

How It Works

  1. Convert the Number to a String:
  • Integer.toString(num) converts the integer num into a string.
  • Example: If num is 12321, then original will be "12321".

2. Reverse the String:

  • new StringBuilder(original).reverse().toString() creates a new StringBuilder with the original string, reverses it, and converts it back to a string.
  • Example: If original is "12321", then reversed will also be "12321".

3. Compare the Original and Reversed Strings:

  • original.equals(reversed) checks if the original string is the same as the reversed string.
  • If they are the same, the number is a palindrome; otherwise, it’s not.

Why This Method is Easier

  • No Math Operations: You don’t need to extract digits or perform any arithmetic operations.
  • String Comparison: Comparing strings is straightforward and easy to understand.
  • Readable Code: The code is more readable, especially if you’re comfortable with string manipulation.

--

--