Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Happy Number

Updated 21 Oct 2026

A happy number is a number that finally reaches 1 when it is repeatedly replaced by the sum of the squares of its digits. This note gives the method, an ISC-style Java program written in the main method alone, sample runs and a dry run.

Definition

Take a number, square each of its digits and add the squares. Repeat the process with the new number. If the value becomes 1 the number is a Happy Number. If it never becomes 1 it falls into a repeating loop and the number is not happy. The number 1 is happy, while 0 and negative numbers are treated as invalid input.

  • $19$: $1^2 + 9^2 = 82$, $8^2 + 2^2 = 68$, $6^2 + 8^2 = 100$, $1^2 + 0^2 + 0^2 = 1$. So $19$ is happy.
  • $7$: $49$, $97$, $130$, $10$, $1$. So $7$ is happy.
  • $20$: $2^2 + 0^2 = 4$, then $16$, $37$, $58$, $89$, $145$, $42$, $20$ and back to $4$. The values repeat, so $20$ is not happy.

Algorithm

  1. Accept the number into n. If n is not positive, print an invalid-input message and stop, because 0 would give 0 again and never reach 1.
  2. Copy n into t and repeat while t is neither 1 nor 4:
    • copy t into x and set s = 0;
    • while x > 0, find the last digit with d = x % 10, add its square to s and remove the digit with x = x / 10;
    • replace t by s.
  3. If the loop ended with t equal to 1, print that n is a Happy Number. Otherwise print that it is not.

Why the loop can stop at 4. Every number that is not happy ends in the same cycle $4, 16, 37, 58, 89, 145, 42, 20, 4$. So if the value reaches 4, it will go round this cycle for ever and can never reach 1, which proves the number is not happy. The program therefore stops at 1 (happy) or at 4 (not happy), and there is no need to store the earlier values in an array.

Program

The outer while loop in main replaces t again and again; the inner while loop finds the sum of the squares of the digits.

import java.util.*;

/* Program Happy checks whether a number is a happy number, i.e. repeatedly
   replacing it by the sum of the squares of its digits ends in 1. */
public class Happy
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);    // reads input from the keyboard
        System.out.print("Enter a number: ");
        int n = sc.nextInt();                   // the number entered by the user
        if (n <= 0)
        {
            System.out.println("Invalid input. Enter a positive number.");
        }
        else
        {
            int t = n;                          // working copy so that n is not changed
            while (t != 1 && t != 4)            // every unhappy number reaches 4
            {
                int x = t, s = 0;               // s accumulates the sum of squares of the digits of x
                while (x > 0)
                {
                    int d = x % 10;             // extracts the last digit
                    s = s + d * d;              // adds the square of the digit
                    x = x / 10;                 // removes the last digit
                }
                t = s;                          // replace by the sum of squares of its digits
            }
            if (t == 1)
                System.out.println(n + " is a Happy Number");
            else
                System.out.println(n + " is not a Happy Number");
        }
    }
}

Variable description table

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the number typed at the keyboard
nintThe number entered by the user
tintWorking copy of n that is replaced again and again by the sum of squares
xintCopy of t whose digits are squared, reduced by 1 digit each pass
sintAccumulates the sum of the squares of the digits of x
dintLast digit of x in the current pass of the inner loop

Sample input and output

Sample 1

Enter a number: 19
19 is a Happy Number

Sample 2

Enter a number: 20
20 is not a Happy Number

Sample 3

Enter a number: 0
Invalid input. Enter a positive number.

One value per line

Dry run

Input: n = 19. Set t = 19; each row is one pass of the outer while loop, with the inner loop working digit by digit.

Passt beforeDigits squaredt aftert is 1 or 4?
1191 + 8182No
28264 + 468No
36836 + 64100No
41001 + 0 + 01Yes, loop stops

t == 1 is true, so the program prints 19 is a Happy Number, the same as the real run above.

Variations

  1. Range "Accept two integers m and n and display all the happy numbers between m and n."
  2. Unhappy number "Check whether a number is an unhappy number, that is, one that never reaches 1."
  3. Recursion "Write the happy number check using a recursive method that calls itself with the sum of the squares of the digits."