Sad Number in Java — Definition, Explanation, Examples, and Program

Learn what a Sad Number is in Java with clear explanation and logic. This blog covers the definition of a Sad Number, how it differs from a Happy Number, examples, and a complete Java program. Includes practice challenges to strengthen your number theory concepts.

Sad Number in Java

A Sad Number (also known as an Unhappy Number) is a number that never reaches 1 when we keep replacing it with the sum of the squares of its digits. Instead, it enters a repeating loop forever.

Before writing the Java program, let's deeply understand how Sad Numbers behave. We’ll walk step-by-step, ask interesting questions, and prove why certain assumptions fail.

Example: Step-by-Step Dry Run

  • Example: 4
    Steps:
    4² = 16
    1² + 6² = 37
    3² + 7² = 58
    5² + 8² = 89
    8² + 9² = 145
    1² + 4² + 5² = 42
    4² + 2² = 20
    2² + 0² = 4 (Loop repeats)

    ✔️ 4 is a Sad Number

A Curious Thought… 🤔

Looking at the example above, you might wonder:

“Why don’t we just check whether the original number (like 4) comes again in the loop? If it comes again → it’s a Sad Number!”

This sounds logical at first… but unfortunately, it does not work in most cases.

Let’s Prove This Wrong

Consider another number:

  • Example: 12
    Steps:
    1² + 2² = 5
    5² = 25
    2² + 5² = 29
    2² + 9² = 85
    8² + 5² = 89
    → From here, the cycle becomes:
    89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89 → ...

    The important part:
    12 never appears again!

    ✔️ 12 is still a Sad Number

So the loop does not care about the original number. It only enters the fixed unhappy cycle.

The Universal Sad Number Loop

All Sad Numbers eventually fall into this 8-number loop:

4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → (back to 4)

So checking whether the original number returns will fail for almost all values.

So How Do We Detect a Sad Number Correctly?

We simply need to detect when a loop begins. And to do that, we need to track numbers we have already seen.

Enter HashSet — Our Loop Detector

A HashSet in Java is a data structure that:

  • Stores unique values
  • Allows fast lookup (O(1))
  • Helps us detect repetition instantly

If a number appears again → a loop has started → the number is Sad.

We don’t care if the loop returns to the original number. We only care if it returns to any number seen before.

Java Program to Check Sad Number

import java.util.HashSet;

public class SadNumber {

    public static int digitSquareSum(int num) {
        int sum = 0;
        while (num > 0) {
            int digit = num % 10;
            sum += digit * digit;
            num /= 10;
        }
        return sum;
    }

    public static void main(String[] args) {
        int num = 12; // Change this to test other numbers

        HashSet seen = new HashSet<>();

        while (num != 1 && !seen.contains(num)) {
            seen.add(num);
            num = digitSquareSum(num);
        }

        if (num == 1) {
            System.out.println("Not a Sad Number (It is a Happy Number)");
        } else {
            System.out.println("Sad Number");
        }
    }
}
    

Practice Challenges

Challenge 1:

Write a Java program to print all Sad Numbers between 1 and 300.

Challenge 2:

Count how many Sad Numbers exist in a given range.

Challenge 3 (Special):

Write a Java program to check whether the original number appears again in the loop or not. (Hint: It normally won’t — prove it through output!)