How to Write A “Happy Number” Algorithm

How to Write A “Happy Number” Algorithm

Algorithms play a big role in computer programming. We have highlighted this fact already in one of our previous articles. The most important thing to realize is that to write a decent and flawless code, you must know how to write a flawless algorithm.

An algorithm is universal. The ideas written in a specific algorithm can be brought to life in any programming language. That’s mainly because an algorithm never involves the technicalities of a specific programming language. It’s written in a general sense. In this article, we will show you how to write a “Happy Number” algorithm. This is one of the most common and popular algorithms for beginners who are fairly new to the subject. Let’s begin.

What is a Happy Number?

Numbers do not have emotions…do they? They are abstract. The word “happy,” in this case, is never used in a literal sense. So we can safely assume that finding out numbers who are said to be “happy” makes the finder happy. Hence, the name’s more or less apt. But anyway, let’s get to the technical part of this thing.

You’ll have to be aware of a specific condition at the start to determine whether the number is a happy number of not. The number in your radar MUST be a positive number. Consider that the 1st step.

The 2nd step is to determine the square of each and every digit available within the number.

The 3rd step is, to sum up the individual squares found in the 2nd step.

If the answer found in step 3 is 1, you are free to stop any further calculation. If it’s not, you’ll have to repeat the entire process in a loop until you get the answer 1. (Remember that this is a sequence. By “repeat”, we do not ask you to repeat the entire process with the number provided initially. You’ll have to repeat the entire steps with the result found in step 3. As soon as you get 1, you stop.)

An example can easily clear this thing up.


  • Number: 19.

The number in question is a positive integer. 1st step fulfilled.

So we can easily move on to the next steps-“find the square of the individual digits and add them up.”

1^2 + 9^2 = 1+ 81 = 82.

The answer isn’t 1. So we’ll have to repeat the steps by using the number 82.

8^2 + 2 ^2 = 64 + 4 = 68 (≠1; therefore step needs to be repeated).

6^2 + 8^2= 36 + 64= 100 (≠1; therefore step needs to be repeated).

1^2 +0 +0 = 1.

We have got the answer that we desired. Therefore, the number IS A MAGIC NUMBER.

  • Number: 2.

The number in question is positive, and hence, we can move on to the next steps.

2^2 = 4………………. (i)

4^2= 16

1^2 + 6^2 = 37

3^2 + 7^2 = 58.

5^2 + 8^2= 89

8^2 + 9^2= 145

1^2 + 4^2+ 5^2= 42

4^2 + 2^2= 20

2^2 + 0 = 4………… (ii)

Let’s stop at this stage because we have already found a pattern here. We think you have found it too. Notice the two parts that we have highlighted above by (i) and (ii). You’ll see that we have virtually come back to the same step again. This is virtually an endless loop. One thing’s certain that we won’t be getting our desired answer. The given number is definitely NOT a happy number.

There’s a thumb rule that you can observe especially when “Happy numbers” are concerned. If the result at any step of calculation is a number that lies in the list of 4, 16, 20, 37, 42, 58, 89, 145, the number in question is SURELY a UNHAPPY NUMBER.


The algorithm is pretty simple.

  1. Start
  2. Accept the number as a user input.
  3. If the number is negative,

Display “Wrong number. Insert a positive number.”


                                continue to step “4.”

       4. Extract each and every digit from the number taken as a user input.

       5. Find the square of each digit. Add them up and get the result. Store it in a variable (say, Z).

       6. User input no. = Z

       7. If the user input no. is present in the list of 4, 16, 20, 37, 42, 58, 89, 145

Display “The number is L “


                                if user input number’s “1”

                                                Display “ The no. is J”


                                                Repeat step 4.

The happy number algorithm is one for the very basics, but it’s undoubtedly pretty interesting. There are plenty of other algorithms that lie in the same category. Professional tutors can definitely point them out to the beginners thereby, making the subject matter more interesting. With that, we’ll bring this article to an end. Hope you had a good read.

Sudipto Das

Sudipto writes technical and educational content periodically for and backs it up with extensive research and relevant examples. He's an avid reader and a tech enthusiast at the same time with a little bit of “Arsenal Football Club” thrown in as well. He's got a B.Tech in Electronics and Instrumentation.
Follow him on twitter @SudiptoDas1993

Leave a Comment

Your email address will not be published. Required fields are marked *