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 read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.