# Print all Happy numbers within given range in Python

In this tutorial, we will learn to print all the happy numbers within the given range in Python. If you want a Python program to display happy numbers within the range given by the user, you are at the right place. Here, in this tutorial, you will get to know about happy numbers, a method to find happy numbers and a Python program that displays happy numbers using the same method.

## Happy numbers

If the number is greater than 0, then add the squares of the digits of that number. You will get another number as a result. Do the same until you get 1 as a result. Remember, if the result is 1 or 4, then stop the iteration. Finally, if you get 1 as a result, then it is called a happy number. To understand what is a happy number, let’s take an example.

Suppose we take the number 28 as input

• 2^2 + 8^2 = 4 + 64 = 68
• 6^2 + 8^2 = 36 + 64 = 100
• 1^2 + 0^2 + 0^2 = 1 + 0 + 0 = 1

So, we get 1 as a result at the end. Therefore, we can say that the number 28 is a happy number.

Suppose, we take the number 29 as input

• 2^2 + 9^2 = 4 + 81 = 85
• 8^2 + 5^2 = 64 + 25 = 89
• 8^2 + 9^2 = 64 + 81 = 145
• 1^2 + 4^2 + 5^2 = 1 + 16 + 25 = 42
• 4^2 + 2^2 = 16 + 4 = 20
• 2^2 + 0^2 = 4 + 0 = 4

Finally, we get 4 as a result at the end. So, the number 29 is not a happy number.

## Method to find whether the given number is a happy number

So, to check whether the given number is a happy number or not, follow the steps given below-

• Firstly, finding the sum of the squares of its digits-
• Declare a variable ‘sum’ and initialize it with 0.
• If the number is greater than 0, divide it by 10 to get the remainder.
• Store the remainder in variable ‘digit’.
• Calculate the square of the number in ‘digit’.
• Add the result to the value in ‘sum’.
• Divide the number by 10.
• Finally, check whether the given number is a happy number-
• If the result is not equal to 1 or 4, continue the above steps with the subsequent results till you get 1 or 4 in ‘sum’.
• If the final result is 1, the number is a happy number.

## Python program to print happy numbers within the given range

Now, we will see a Python program to print all the happy numbers within the given range. Firstly, we will take the lower and upper bound of the range from the user. Then, using a ‘for’ loop, we find the happy numbers from lower bound to the upper bound numbers. Finally, we print the number if it is a happy number. The Python program for the same is given below-

```def check_happy(number):
remainder = 0
happy_num = 0;
while(number > 0):
remainder = number%10;
happy_num = happy_num + (remainder*remainder);
number = number//10;
return happy_num;

lower = int(input("ENTER LOWEST VALUE : "))
upper = int(input("ENTER HIGHEST VALUE : "))
print("HAPPY NUMBERS WITHIN RANGE({},{}) ARE -".format(lower,upper))
for i in range(lower,upper+1):
happy_num = i
while(happy_num != 1 and happy_num != 4):
happy_num = check_happy(happy_num)
if(happy_num == 1):
print(i,end=" ")```

Here, we define a function ‘check_happy’ which returns the sum of the squares of the digits of the number. The variables ‘lower’ and ‘upper’ store the lower and upper bound numbers respectively. If the final result returned by the ‘check_happy’ function is 1, then the number is a happy number and is displayed.

#### Python program output

The above program displays all the happy numbers within the given range. The output of the above Python program is-

```[email protected]:~/python\$ python3 happy.py
ENTER LOWEST VALUE : 5
ENTER HIGHEST VALUE : 50
HAPPY NUMBERS WITHIN RANGE(5,50) ARE -
7 10 13 19 23 28 31 32 44 49
[email protected]:~/python\$```

In the above program execution, the range is (5, 50). So, within this range, there are 10 happy numbers – 7, 10, 13, 19, 23, 28, 31, 32, 44, and 49. Hence, the program prints these happy numbers as the output.

Also read: Find the HCF of two numbers in Python