Happy Number in Java

Learn what a Happy Number is and how to check whether a number is happy or not in Java. This guide explains the concept, algorithm, working process, dry run, and provides multiple Java programs for beginners.

Introduction

A Happy Number is a special type of number defined using a repetitive process: take a number, replace it with the sum of the squares of its digits, and repeat the process until the number becomes either 1 (happy) or loops endlessly in a cycle that does not include 1 (unhappy).

For example, 19 is a Happy Number because:

Since it reaches 1, it is a happy number.

What is a Happy Number?

A happy number is defined by the following process:

  1. Start with a positive integer.
  2. Replace the number by the sum of the squares of its digits.
  3. Repeat the process.
  4. If the number eventually becomes 1, it is a happy number.
  5. If it loops endlessly and never reaches 1, it is not a happy number.

Algorithm

  1. Initialize an empty set to store visited numbers.
  2. Repeat: calculate the sum of squares of digits.
  3. If the result becomes 1 → return true (happy number).
  4. If the result appears again (loop detected) → return false.

Java Program to Check Happy Number


import java.util.HashSet;

public class HappyNumber {

    static boolean isHappy(int n) {
        HashSet seen = new HashSet<>();

        while (n != 1 && !seen.contains(n)) {
            seen.add(n);
            n = sumOfSquares(n);
        }

        return n == 1;
    }

    static int sumOfSquares(int n) {
        int sum = 0;

        while (n > 0) {
            int digit = n % 10;
            sum += digit * digit;
            n /= 10;
        }

        return sum;
    }

    public static void main(String[] args) {
        int num = 19;

        if (isHappy(num))
            System.out.println(num + " is a Happy Number.");
        else
            System.out.println(num + " is NOT a Happy Number.");
    }
}

Dry Run Example (19)

Step n Explanation
1 19 1² + 9² = 82
2 82 8² + 2² = 68
3 68 6² + 8² = 100
4 100 1² + 0² + 0² = 1

Happy Number Using Recursion


import java.util.HashSet;

public class HappyNumberRecursion {

    static HashSet seen = new HashSet<>();

    static boolean isHappy(int n) {
        if (n == 1) return true;
        if (seen.contains(n)) return false;

        seen.add(n);
        return isHappy(sumOfSquares(n));
    }

    static int sumOfSquares(int n) {
        int sum = 0;

        while (n > 0) {
            int digit = n % 10;
            sum += digit * digit;
            n /= 10;
        }

        return sum;
    }

    public static void main(String[] args) {
        int number = 7;

        if (isHappy(number))
            System.out.println(number + " is a Happy Number.");
        else
            System.out.println(number + " is NOT a Happy Number.");
    }
}

Conclusion

Happy Numbers are an important topic for beginners and are frequently asked in programming interviews. This program helps build logic using loops, sets, recursion, and number manipulation techniques.