Check Whether a Number is a Lychrel Number Candidate in Java

Topic

A Lychrel number candidate is a number that does not form a palindrome through the iterative process of reversing its digits and adding the result. While it's unknown if true Lychrel numbers exist, numbers like 196 are suspected candidates because they don't form palindromes even after many iterations.

In this program, we’ll check whether a number appears to be a Lychrel candidate by running the reverse-and-add process up to a certain number of iterations (e.g., 50). If no palindrome appears, we consider it a candidate.

Examples

Example 1:

Input: 56
Step 1: 56 + 65 = 121 (Palindrome)
Output: Not a Lychrel candidate

Example 2:

Input: 196
After 50 iterations, no palindrome is formed.
Output: Lychrel candidate

Example 3 (Edge case):

Input: 0
Palindrome already — Output: Not a Lychrel candidate

Interviewer Expectations

The interviewer is likely testing your ability to:

  • Implement reverse logic for integers
  • Use loops and conditions to control iterations
  • Understand palindromic logic
  • Handle large integers or limits safely

Approach

1. Create a function to reverse a number.
2. Add the number and its reverse.
3. Check if the result is a palindrome.
4. Repeat this for a maximum number of iterations (like 50).
5. If no palindrome is found, it's a Lychrel candidate.

Dry Run: Input = 56

  • 56 + 65 = 121 → is a palindrome ✅
  • So, it's not a Lychrel candidate.

Java Program

public class LychrelChecker {

    public static void main(String[] args) {
        int number = 196;
        int limit = 50;
        boolean isLychrel = true;

        for (int i = 0; i < limit; i++) {
            int reversed = reverseNumber(number);
            number = number + reversed;
            if (isPalindrome(number)) {
                isLychrel = false;
                System.out.println("Not a Lychrel candidate. Palindrome found at iteration " + (i + 1));
                break;
            }
        }

        if (isLychrel) {
            System.out.println("Lychrel candidate. No palindrome found in " + limit + " iterations.");
        }
    }

    static int reverseNumber(int num) {
        int reversed = 0;
        while (num != 0) {
            reversed = reversed * 10 + num % 10;
            num /= 10;
        }
        return reversed;
    }

    static boolean isPalindrome(int num) {
        return num == reverseNumber(num);
    }
}
Lychrel candidate. No palindrome found in 50 iterations.

Possible Followup Questions with Answers

Q1: Can we encounter integer overflow with large iterations?

Yes. Use long or even BigInteger in real-world scenarios to avoid overflow, especially with inputs like 196 that grow quickly.

Q2: Can a number be a palindrome before the first addition?

Yes. If the number is already a palindrome (e.g., 121), it's definitely not a Lychrel candidate.

Q3: How can we extend this to check for all Lychrel candidates under 10,000?

Use a loop from 1 to 9999, and apply the same logic inside that loop.

Q4: What are some other "special numbers" interview questions?

You can also be asked to write programs for:

  • Armstrong Numbers
  • Perfect Numbers
  • Palindrome Numbers
  • Happy Numbers


Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...