Python Program to check the number is a happy number

In this simple python program, we have to check for a happy number. It's a beginner-level python program.

To understand this example, you should have knowledge of the following Python programming topics:

What is a happy number?

In this python program example, we need to check for a happy number. A number is said to be a Happy Number if the sum of the squares of all the digits in that number yield one when it is repeatedly doing. If that results in endless cycles of resultant 4, then that number is said to be an unhappy number. Let us take an example to check if a number is a happy number or not to understand clearly.

Take number 32. Now, let's split the number and check for a Happy number or an unhappy number.

  • 32 + 22 = 13, We split the number and take the sum of squares of the digits in that number
  • 12 + 32 = 10, again split the resultant number 13 and take the sum of the squares of 1 and 3
  • 12 + 02 = 1, again we split the number 10 and take the sum of the square of each digit
  • the result is 1, so the given number 32 is Happy number

We also have a program to display happy numbers between the range 1 and 100 in our python program collection.

How to check for happy numbers in python?

To solve this python program, we apply the logic using a while loop in python until the result from the user-defined function is 1 or 4.  Use a user-defined function in python where we pass the number from the main program to the function then we initialize two variables for remainder and sum as zero. Then check for a happy number by starting a while loop where the number is greater than zero.

Take the remainder of the number by using the mod operator by 10, and we add the sum with the square of the remainder. Then we divide the number by 10 to split the number and take the next digit. Finally, we return sum as a result. Then check the result is 1, using if condition in python. If so print the number is a happy number. else unhappy number.

ALGORITHM

STEP 1: Predefine a number to check for a happy number. We can use the input function also to receive input from the user.

STEP 2: Initialize the variable for storing the result of the python program.

STEP 3: Open a while loop until the result is 1 or 4, and call the user-defined function for checking the number is a happy number or not in the python programming language. We pass the number as a parameter to the function.

USER DEFINED FUNCTION isHNumber(num)

STEP 1: Initialize the variables rem and sum as zero.

STEP 2: Open a while loop until the num is equal to zero.

STEP 3: We take the remainder using the mod operator.

STEP 4: Sum is getting added to the sum with the square of the remainder.

STEP 5: Split the number by dividing the num by 10 and return the sum.

Python Source Code

                                          def isHNumber(num):
   rem = sum = 0
   while(num > 0):
      rem = num
      sum = sum + (rem*rem)
      num = num//10
   return sum;
num = 86
result = num
while(result != 1 and result != 4):
   result = isHNumber(result);
print("The number is being checked")
if(result == 1):
   print(str(num) + " is a happy number");
elif(result == 4):
   print(str(num) + " isn't a happy number");
                                      

OUTPUT

82 is a happy number