Checking Palindrome Number Using While Loop in Java

Determining whether a number is a palindrome or not is a common programming task. In this tutorial, we will explore how to check if a number is a palindrome using a while loop in Java. This step-by-step guide is specifically designed for learners and aims to explain the concept, syntax, and provide examples of using a while loop to check for palindromic numbers.

Understanding Palindrome Numbers

A palindrome number is a number that remains the same when its digits are reversed. For example, 121 and 12321 are palindrome numbers.

Syntax of the While Loop

The syntax for a while loop in Java is as follows:

while (condition) {
// Code block to be executed repeatedly while the condition is true
// Typically, there should be a way to modify the condition inside the loop

Implementing the Program: Step-by-Step

Let’s break down the implementation of the program into step-by-step instructions:

  • Step 1: Take an integer input for which you want to check for palindrome.
  • Step 2: Initialize a variable num with the input number.
  • Step 3: Initialize a variable reverse to store the reverse of the number and set it to 0.
  • Step 4: Use a while loop with the condition num > 0 to iterate until num becomes 0.
  • Step 5: Extract the last digit of num using the modulus operator % and add it to reverse.
  • Step 6: Multiply reverse by 10 and add the extracted digit to reverse the order.
  • Step 7: Update num by dividing it by 10, discarding the last digit.
  • Step 8: Repeat steps 5 to 7 until num becomes 0.
  • Step 9: After the loop ends, compare the original number with reverse to check if they are equal.
  • Step 10: Print whether the number is a palindrome or not based on the comparison result.

    Example: Checking Palindrome Number

    Let’s consider an example where we want to check if the number 12321 is a palindrome:

    int num = 12321;
    int originalNum = num;
    int reverse = 0;

    while (num > 0) {
    int digit = num % 10;
    reverse = (reverse * 10) + digit;
    num /= 10;

    if (originalNum == reverse) {
    System.out.println(originalNum + ” is a palindrome number.”);
    } else {
    System.out.println(originalNum + ” is not a palindrome number.”);

    The output for this example will be: “12321 is a palindrome number.”


    Using a while loop in Java, we can efficiently check if a number is a palindrome or not. The program we discussed simplifies this task by reversing the digits and comparing them with the original number. Understanding the syntax and examples provided will enable you to implement similar logic in your Java programs effectively.

    Continue honing your skills and delving into the vast potential of while loops in Java programming. Enjoy your coding journey!