Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

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

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

For more detailed explanations, refer to GeeksforGeeks and Stack Overflow discussion on reversing numbers.

Java

  1. C++ While and Do‑While Loops – Mastering Repetition in Your Code
  2. Master Java For Loops: Syntax, Examples, and Best Practices
  3. Mastering the Java Enhanced For Loop: Syntax, Examples, and Best Practices
  4. Mastering Java While & Do‑While Loops: Step‑by‑Step Tutorial
  5. Master C Loops: For, While, and Do‑While Explained with Practical Examples
  6. Java For‑Each Loop: Simplifying Array Iteration Without Counters
  7. Java Program to Determine If a Number Is Prime
  8. Java Program to List Prime Numbers from 1 to 100 – Step‑by‑Step Guide
  9. Generate Fibonacci Sequence in Java: For, While, and Recursive Examples
  10. Java Program to Identify Armstrong Numbers Using For Loop