# 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:

- Python if…else Statement
- Loops in python
- Functions in Python

### 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

3^{2 }+ 2^{2} + 0^{2 }= 13

1^{2} + 3^{2} = 10

1^{2} + 0^{2} = 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

*and*

**digit***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*

**sum***in which we find each digit and then square it. We find each digit using*

**while loop***then we square it by multiplying the digit with itself. We also divide*

**mod(%)***by 10 so that we can find the next digit. This continues till*

**n***. This link can be referred to for any further doubts. We return the*

**n <= 0***after the loop finishes.*

**sum**In the driver code, we first ask the user to input the number using * int(input(“Enter a number: “))*. Here,

*prints the message on the console and also reads the input given as a string. But we want the input to be an*

**input()***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*

**int()***so that we can use it to compare with the sum later.*

**result**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

*is either 1 or 4. Once we exit the loop. We use an*

**result***to check if the output of*

**if…else statement***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.*

**isHappyNumber()**### 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.

Armstrong Number Check of a given number in input using Python

## Leave a Reply