Check If A Number Is A Happy Number

In this tutorial, we will be looking at a python program to check if a number is a happy number or not. We will be using the following Python concepts to solve this problem:

Happy Number

A number is said to be a Happy Number if the repeated sum of the digits squared is equal to 1. If on repeating this process we get the result as 1 then it is a happy number. If the result is 4 then it goes in an endless loop and is not a happy number. Let’s take an example to understand it better.

Number = 320
32 + 22 + 02 = 13
12 + 32 = 10
12 + 02 = 1

Examples of Happy Numbers: 7, 28, 100, etc.
Examples of Unhappy Numbers: 4, 16, 37, 58, etc.

Check if a number is a happy number or not in Python

def isHappyNumber(n):    
    digit = sum = 0    
    while(n > 0):    
        digit = n % 10 
        sum = sum + (digit * digit)    
        n = n // 10   
    return sum    
        
num = int(input("Enter a number: "))    
result = num    
     
while(result != 1 and result != 4):    
    result = isHappyNumber(result)   
     
if(result == 1):    
    print(num, " is a Happy Number!!!")   
else:    
    print(num, " is an Unhappy Number!!!")

We have defined isHappyNumber() to check if the number is a happy number or not. We initiate digit and sum as 0. This is so that we can use them later and also signify that they are of int datatype. To calculate the sum we start a while loop in which we find each digit and then square it. We find each digit using mod(%) then we square it by multiplying the digit with itself. We also divide n by 10 so that we can find the next digit. This continues till n <= 0. This link can be referred to for any further doubts. We return the sum after the loop finishes.

In the driver code, we first ask the user to input the number using int(input(“Enter a number: “)). Here, input() prints the message on the console and also reads the input given as a string. But we want the input to be an int so that we can perform mathematical operations on them and for that, we use int(). We also make a copy of the inputted number in result so that we can use it to compare with the sum later.

If the inputted number is 1 or 4 we don’t need to use isHappyNumber(). This is because 1 means that it is a happy number and if it is 4 then it is going to end in an endless cycle and it is an unhappy cycle.

If the number is not 1 or 4 we pass this value as a parameter to isHappyNumber(). This loop repeated until the result is either 1 or 4. Once we exit the loop. We use an if…else statement to check if the output of isHappyNumber() is 1 or 4. If it’s 1 we will print that it is a Happy Number and if it is 4 then we print that it is an Unhappy Number.

Output

Enter a number: 100
100 is a Happy Number

Enter a number: 37
100 is an Unhappy Number

So here it is, a simple program to check if a number is a happy number or not.

Also read,
Find first Sunday of the month in Python

Armstrong Number Check of a given number in input using Python

Palindrome Check of a given word or number using python

Leave a Reply