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
- Accept the number into
n. Ifnis not positive, print an invalid-input message and stop, because 0 would give 0 again and never reach 1. - Copy
nintotand repeat whiletis neither 1 nor 4:- copy
tintoxand sets = 0; - while
x > 0, find the last digit withd = x % 10, add its square tosand remove the digit withx = x / 10; - replace
tbys.
- copy
- If the loop ended with
tequal to 1, print thatnis 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
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the number typed at the keyboard |
n | int | The number entered by the user |
t | int | Working copy of n that is replaced again and again by the sum of squares |
x | int | Copy of t whose digits are squared, reduced by 1 digit each pass |
s | int | Accumulates the sum of the squares of the digits of x |
d | int | Last 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 NumberSample 2
Enter a number: 20
20 is not a Happy NumberSample 3
Enter a number: 0
Invalid input. Enter a positive number.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.
| Pass | t before | Digits squared | t after | t is 1 or 4? |
|---|---|---|---|---|
| 1 | 19 | 1 + 81 | 82 | No |
| 2 | 82 | 64 + 4 | 68 | No |
| 3 | 68 | 36 + 64 | 100 | No |
| 4 | 100 | 1 + 0 + 0 | 1 | Yes, loop stops |
t == 1 is true, so the program prints 19 is a Happy Number, the same as the real run above.
Variations
- Range "Accept two integers m and n and display all the happy numbers between m and n."
- Unhappy number "Check whether a number is an unhappy number, that is, one that never reaches 1."
- Recursion "Write the happy number check using a recursive method that calls itself with the sum of the squares of the digits."