Learn how to check whether a number is a palindrome in Java using simple code and detailed step-by-step explanation. Perfect for beginners in Java programming.
A palindrome number is a number that reads the same backward as forward.
Examples:
121
is a palindrome (reads same from left to right and right to left)
123
is not a palindrome
1221
is a palindrome
Let’s start with a simple Java program to check if a number is a palindrome:
import java.util.Scanner;
public class PalindromeChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Step 1: Take input from user
System.out.print("Enter a number: ");
int number = scanner.nextInt();
// Step 2: Store original number for comparison later
int originalNumber = number;
int reversedNumber = 0;
// Step 3: Reverse the number
while (number != 0) {
int digit = number % 10; // Get the last digit
reversedNumber = reversedNumber * 10 + digit; // Build reversed number
number = number / 10; // Remove last digit
}
// Step 4: Compare original and reversed numbers
if (originalNumber == reversedNumber) {
System.out.println(originalNumber + " is a palindrome.");
} else {
System.out.println(originalNumber + " is not a palindrome.");
}
scanner.close();
}
}
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
We use the Scanner
class to take input from the user.
The number entered by the user is stored in a variable called number
.
int originalNumber = number;
We store the original number in another variable so that we can compare it later with the reversed number.
int reversedNumber = 0;
while (number != 0) {
int digit = number % 10;
reversedNumber = reversedNumber * 10 + digit;
number = number / 10;
}
Let’s break this loop down:
number % 10
gets the last digit of the number.
Multiply reversedNumber
by 10 to shift digits to the left and add the new digit.
Divide number
by 10 to remove the last digit (since it's already used).
Example:
If the user enters 12321
:
First digit: 1
Second digit: 2
...
Eventually reversedNumber
will become 12321
again.
if (originalNumber == reversedNumber) {
System.out.println(originalNumber + " is a palindrome.");
} else {
System.out.println(originalNumber + " is not a palindrome.");
}
We check if the reversed number is equal to the original number.
If yes, it's a palindrome.
If not, it's not a palindrome.
Enter a number: 1221
1221 is a palindrome.
Enter a number: 456
456 is not a palindrome.
Negative numbers are not palindromes because they include a minus sign -
, which cannot appear at the end when reversed.
You can handle this in your code:
if (originalNumber < 0) {
System.out.println("Negative numbers are not considered palindromes.");
}
Understand how the while
loop works with %
and /
.
Practice the same logic with strings later (for word palindromes).
Test with different inputs like 0
, single digits, even and odd-length numbers.
Checking whether a number is a palindrome in Java is a great beginner-level programming problem. It helps you understand loops, mathematical operations, and logic building.
With the code and step-by-step explanation above, you should now be able to write and understand a palindrome number checker with confidence!
Thank you for visiting! Enjoy exploring our diverse collection of blogs, crafted with passion and insight to inspire and inform. Happy reading!