Enter a number: 121
The number is a Palindrome.
import java.util.Scanner;
public class PalindromeNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int original = num;
int reverse = 0;
while (num != 0) {
int digit = num % 10;
reverse = reverse * 10 + digit;
num = num / 10;
}
if (original == reverse) {
System.out.println("The number is a Palindrome.");
} else {
System.out.println("The number is NOT a Palindrome.");
}
sc.close();
}
}
Store the original number.
Reverse the number using a while loop.
Compare original number with reversed number.
If both are equal → Palindrome
Otherwise → Not Palindrome
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.