Check Palindrome Number

This program checks if a number is palindrome using while loop.

Problem Statement

Write a Java program that checks if 121 is a palindrome using while loop.

Source Code

1 public class PalindromeCheck {
2 public static void main(String[] args) {
3 int num = 121, temp = num, rev = 0;
4 while(temp > 0) {
5 rev = rev * 10 + temp % 10;
6 temp /= 10;
7 }
8 if(rev == num) {
9 System.out.println(num + " is palindrome - Eduinq");
10 } else {
11 System.out.println(num + " is not palindrome - Eduinq");
12 }
13 }
14 }

Program Output

121 is palindrome - Eduinq

Explanation

The program reverses the number by extracting digits and appending them to rev. After processing all digits, it compares rev with the original number. If equal, it is palindrome. This demonstrates how while loops can check palindrome numbers by reversing digits. Shows how while loop can check palindrome numbers.

Quick Links to Explore