Happy Number Checker

Test if a number is a happy number.

Result

Happy number
4

How it is calculated

A happy number is one that ends at 1 when you repeat a simple step: replace the number with the sum of the squares of its digits. The tool does exactly this loop, n → Σ dᵢ², counting each step, and stops when it reaches 1 or when a value repeats, which means it is stuck in a cycle. If 1 is reached the number is called happy and the step count is shown; otherwise it is not happy and the steps show a dash. Whole numbers from 1 to 1,000,000,000 are accepted, and decimals are cut to the integer part. Every unhappy number eventually falls into the same cycle that contains 4.

Worked example

Test 19. Step 1: 1² + 9² = 1 + 81 = 82. Step 2: 8² + 2² = 64 + 4 = 68. Step 3: 6² + 8² = 36 + 64 = 100. Step 4: 1² + 0² + 0² = 1. The sequence reached 1 after 4 steps, so 19 is a happy number. By contrast, 4 goes 16, 37, 58, 89, 145, 42, 20 and back to 4, so it is not happy.

Try this example →

Common mistakes

  • Squaring the whole number instead of each digit. For 19 you add 1² and 9², not 19².
  • Forgetting zero digits. They add nothing, but they must not be skipped when you are counting the digits by hand.
  • Counting the starting number as a step. The tool counts only the replacements, so 19 takes 4 steps and not 5.
  • Expecting a result for 0 or negative numbers. The tool accepts positive whole numbers only, up to 1,000,000,000.
  • Assuming the sequence for an unhappy number can run forever. It always lands in the same loop, so the check always ends.

About this calculator

This tool checks whether a number is a happy number: repeatedly replace the number with the sum of the squares of its digits, and if the process eventually reaches 1, the number is happy (e.g. 19 → 1²+9²=82 → 8²+2²=68 → 6²+8²=100 → 1²+0²+0²=1). Numbers that instead fall into the repeating cycle 4→16→37→58→89→145→42→20→4 are called "unhappy" or "sad."

Programmers use this classic exercise to practice loops, digit manipulation, and cycle detection in coding interviews and algorithm courses, and recreational-math enthusiasts explore happy numbers as a light introduction to number theory. Enter any positive integer to see whether it's happy and the sequence it follows.

Was this helpful?

You can pick up to 3 reactions.

Comments (0)

  • Be the first to comment.

Popular calculators

All Calculators