Home >>Python Programs >Python program to print all happy numbers between 1 and 100

In this example, we will see a Python program through which we can print all happy numbers between 1 to 100.

Any number is said to be a happy number if it yields 1 when replaced by the sum of squares of its digits repeatedly.

**STEP 1:**isHappyNumber() determines whether a given number is happy or not.- If the number is greater than 0, calculate remainder rem by dividing the number with 10.
- Calculate square of rem and add it to a variable sum.
- Divide number by 10.
- Repeat the steps from a to c till the sum of the square of all digits present in number has been calculated
- Finally, return the sum.

**STEP 2:**To display all happy numbers between 1 and 100,- Start a loop from 1 to 100, then make a call to isHappyNumber() method for each value from 1 to 100 and store the return value into a variable result.
- If the result is neither equal to 1 nor 4 then, make a call to isHappyNumber().
- Otherwise, if the result is equal to 1, it implies that the number is happy and display it.

```
#isHappyNumber() will determine whether a number is happy or not
def isHappyNumber(num):
rem = sum = 0;
#Calculates the sum of squares of digits
while(num > 0):
rem = num%10;
sum = sum + (rem*rem);
num = num//10;
return sum;
#Displays all happy numbers between 1 and 100
print("List of happy numbers between 1 and 100: ");
for i in range(1, 101):
result = i;
#Happy number always ends with 1 and
#unhappy number ends in a cycle of repeating numbers which contains 4
while(result != 1 and result != 4):
result = isHappyNumber(result);
if(result == 1):
print(i),
print(" "),
```

Output:

List of happy numbers between 1 and 100:

1

7

10

13

19

23

28

31

32

44

49

68

70

79

82

86

91

94

97

100

List of happy numbers between 1 and 100:

1

7

10

13

19

23

28

31

32

44

49

68

70

79

82

86

91

94

97

100

Python Programs
Python program to print "Hello Python"
Python program to do arithmetical operations
Python program to find the area of a triangle
Python program to swap the values of two variables
Python program to solve quadratic equation
Python program to convert Celsius to Fahrenheit
Python program to convert kilometers to miles
Python program to display calendar
How to generate a random number in python
Python Program to Check Odd or Even
Python Program to check number is positive negative or zero
Python Program to Check Leap Year
Python Program to Check Prime Number
Python Program to Find the Factorial of a Number
Python Print all Prime Numbers in an Interval
Write a python program to display the multiplication table
Python program to print the fibonacci series
Python Program to Find the Factorial of a Number
Python Program to Convert Decimal to Binary Octal and Hexadecimal
Python Program to Display Fibonacci Series Using Recursion
Python Program to Find Armstrong Number between an Interval
Python Program To Find ASCII value of a character
Python Program to Find HCF
Python Program to Find LCM
Python Program to Find the Sum of Natural Numbers
Python Program to Make a Simple Calculator
Python Program to Add Two Matrix
Python Program to Multiply Two Matrix
Remove Punctuation From a String in Python
Python Program to Sort Words in Alphabetic Order
Python Program to Transpose a Matrix
How to Copy an Array into another array in Python
Python program to find the frequency of each element in the array
How to left rotate an array in python
How to Print duplicate elements in array in python
How to print the elements of an array in python
Python program to print the elements of an array in reverse order
Python program to print the elements of an array present on even position
Python program to print the elements of an array present on odd position
Python program to print the largest element in an array
Python program to print the number of elements in an array
Python program to print the smallest element in an array
Find the sum of all elements in an array in python
Python program to right rotate the elements of an array
Python program to sort an array in ascending order
Python program to sort an array in descending order
Python program to print all pronic numbers between 1 and 100
Python program to print all happy numbers between 1 and 100
Python program to determine whether the given number is a Harshad Number.
Python program to check if the given number is Happy Number
Python program to check if the given number is a Disarium Number
Python program to print all disarium numbers between 1 and 100
Python program to check whether a variable is a string or not
Python program to convert a list of characters into a string
Python program to find total number of letters and digits
Python program to find total number of uppercase and lowercase letters
Python program to remove multiple elements from a list using list comprehension
Python program to check if a string is palindrome or not
Capitalizes the first letter of each word in a string in Python
Remove false values from a list in Python
Python program to print all timezones using pytz module
Python program to print current hour, minute, second and microsecond
Python program to print the list of all keywords
Python program to print the version information
Check if a number is a power of another number in Python
Copy odd lines of one file to another file in Python
Draw a pie chart that shows our daily activity in Python
Python Extract numbers from string
Find all permutations of a given string in Python
How to find the execution time of a program in python
Python program to generate the QR code in Python
Python program to get current date
Python program to print current year month and day
How to replace a string with another string in Python

No Sidebar ads