Lychrel Number in Java – Definition, Logic, Examples, and Complete Program

Learn everything about Lychrel Numbers in Java with step-by-step explanation, reverse-add process, complete examples, palindrome checks, and why some numbers never form a palindrome. This guide also explores why checking the original number in the loop fails, how HashSets detect loops, and provides a fully working Java program with practice challenges.

Lychrel Number in Java

A Lychrel Number is a number that never forms a palindrome even after repeatedly applying the Reverse + Add process. Although no number has been mathematically proven to be a true Lychrel Number, certain numbers (like 196) are famous because no palindrome has ever formed, even after millions of iterations.

In programming, we use a practical definition:
If a number does not form a palindrome within 500 Reverse + Add iterations, we consider it a Lychrel candidate.

What is the Reverse + Add Process?

  • Take a number
  • Reverse the digits
  • Add the reversed number to the original
  • If the result becomes a palindrome → Not a Lychrel Number
  • If no palindrome appears within the limit → Lychrel candidate

Example 1: 56

  • Step 1: Reverse 56 → 65
  • 56 + 65 = 121

Since 121 is a palindrome, 56 is NOT a Lychrel Number.


Example 2: 89 (A Very Interesting Case)

89 is known for taking 24 iterations to reach a palindrome.

  • Step 1: 89 + 98 = 187
  • Step 2: 187 + 781 = 968
  • Step 3: 968 + 869 = 1837
  • … continues …
  • Final: 8813200023188 (palindrome)

So 89 is NOT a Lychrel Number, though it behaves like one for a while.


Example 3: 196 – The Famous Suspected Lychrel Number

The number 196 is the most famous Lychrel candidate. Despite millions of Reverse + Add iterations tested by mathematicians and supercomputers, no palindrome has ever been found.

This makes 196 an excellent example for Lychrel behavior in programming tasks.


Full Dry Run Example

Let’s take a small number and trace all the steps clearly.

Example: 4

  • Step 1: Reverse 4 → 4, so 4 + 4 = 8
  • Step 2: Reverse 8 → 8, so 8 + 8 = 16
  • Step 3: Reverse 16 → 61 → 16 + 61 = 77
  • 77 is a palindrome

Therefore, 4 is NOT a Lychrel Number.


Java Program to Check Lychrel Number

public class LychrelNumber {

    // Reverse a number
    static long reverse(long n) {
        long rev = 0;
        while (n > 0) {
            rev = rev * 10 + n % 10;
            n /= 10;
        }
        return rev;
    }

    // Check if a number is palindrome
    static boolean isPalindrome(long n) {
        return n == reverse(n);
    }

    // Check if a number is a Lychrel candidate
    static boolean isLychrel(long n) {
        long temp = n;

        for (int i = 0; i < 500; i++) {
            long rev = reverse(temp);
            temp = temp + rev;

            if (isPalindrome(temp)) {
                return false; // Not a Lychrel Number
            }
        }

        return true; // Lychrel candidate
    }

    public static void main(String[] args) {
        long n = 196;

        if (isLychrel(n))
            System.out.println(n + " is a Lychrel Number");
        else
            System.out.println(n + " is NOT a Lychrel Number");
    }
}
  

Practice Challenges

  1. Write a Java program to: Print every step of the Reverse + Add process until a palindrome is found (or until 500 iterations).
  2. Modify the program to: Count the exact number of iterations required to form a palindrome.
  3. For numbers from 1 to 500: Print all Lychrel candidates based on the 500-iteration limit.