Java Palindrome Number Checker: Algorithms Using While and For Loops
What Is a Palindrome Number?
A palindrome number reads the same forward and backward. Examples include 121, 393, and 48084. They exhibit vertical axis symmetry and are often used in algorithmic puzzles.
In Java, palindrome checks are common in interview questions and coding challenges. The algorithm is straightforward but can be implemented in multiple ways.
Algorithm Overview
- Read the input integer.
- Reverse its digits and store the result.
- Compare the reversed number to the original.
- If they match, the number is a palindrome; otherwise, it is not.
Checking with a While Loop
Below is a concise Java program that uses a while loop to reverse the number and determine if it is a palindrome. The code prints intermediate steps to illustrate the process.
package com.guru99;
public class PalindromeNum {
public static void main(String[] args) {
int inputNumber = 171; // number to check
int reversed = 0;
int temp = inputNumber;
while (temp > 0) {
int lastDigit = temp % 10;
reversed = (reversed * 10) + lastDigit;
temp /= 10;
}
System.out.println("Reversed number: " + reversed);
if (reversed == inputNumber) {
System.out.println("Number is a palindrome.");
} else {
System.out.println("Number is not a palindrome.");
}
}
}
Output
Reversed number: 171 Number is a palindrome.
Checking with a For Loop
The same logic can be expressed with a for loop, which may be preferable in contexts where the loop counter is more naturally expressed as a decrementing index.
package com.guru99;
public class PalindromeNum {
public static void main(String[] args) {
int inputNumber = 185; // number to check
int reversed = 0;
int temp = inputNumber;
for (; temp > 0; temp /= 10) {
int lastDigit = temp % 10;
reversed = (reversed * 10) + lastDigit;
}
System.out.println("Reversed number: " + reversed);
if (reversed == inputNumber) {
System.out.println("Number is a palindrome.");
} else {
System.out.println("Number is not a palindrome.");
}
}
}
Output
Reversed number: 581 Number is not a palindrome.
Considerations and Best Practices
- Negative Numbers: By definition, negative numbers are not considered palindromes because of the leading minus sign. Return
falseimmediately if the input is negative. - Overflow: Reversing a 32‑bit integer can overflow. For production code, use
longor check for overflow before each multiplication. - String Approach: Converting the number to a string and comparing it to its reverse is often simpler and less error‑prone, especially for very large numbers.
For more detailed explanations, refer to GeeksforGeeks and Stack Overflow discussion on reversing numbers.
Java
- C++ While and Do‑While Loops – Mastering Repetition in Your Code
- Master Java For Loops: Syntax, Examples, and Best Practices
- Mastering the Java Enhanced For Loop: Syntax, Examples, and Best Practices
- Mastering Java While & Do‑While Loops: Step‑by‑Step Tutorial
- Master C Loops: For, While, and Do‑While Explained with Practical Examples
- Java For‑Each Loop: Simplifying Array Iteration Without Counters
- Java Program to Determine If a Number Is Prime
- Java Program to List Prime Numbers from 1 to 100 – Step‑by‑Step Guide
- Generate Fibonacci Sequence in Java: For, While, and Recursive Examples
- Java Program to Identify Armstrong Numbers Using For Loop