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 one number 32. Now, let's split the number and check for a Happy number or an unhappy number.

  1. 3**2 + 2**2 = 13 [ We split the number and take the sum of the digits' squares in that number, so here 3 square + 2 square = 13. ]
  2. 1**2 + 3**2 = 10 [ again split the resultant number 13 to 1 **2 and 3**2 and take the sum of the squares of two digits and the resultant number is 10]
  3. 1**2 + 0**2 = 1 [ again we split the result of the last iteration and do the same as taking the sum of the square of each digit and the result is 1, so the given number 32 is Happy number ]

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 reminder 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 reminder square. 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 as a happy number. else not a happy 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 reminder and sum the variable as zero.

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

STEP 3: We take the reminder 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 number by 10 and return the sum.

Python Source Code

                                          def isHNumber(num):
   rem = sum = 0
   while(num > 0):
      rem = num%10
      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