Python Program to Print Happy Numbers from 1 to 100

Write a Python program to print Happy numbers from 1 to 100 or within a range. This example accepts the minimum and maximum values and displays the Happy numbers within that range.

import math
def digitsSquareSum(Number):
    Sum = rem = 0
    while Number > 0:
        rem = Number % 10
        Sum = Sum + math.pow(rem, 2)
        Number = Number // 10
    return Sum


minHpy = int(input("Enter the Minimum Happy Number = "))
maxHpy = int(input("Enter the Maximum Happy Number = "))

print("\nThe List of Happy Numbers from {0} and {1}".format(minHpy, maxHpy)) 
for i in range(minHpy, maxHpy + 1):
    Temp = i

    while Temp != 1 and Temp != 4:
        Temp = digitsSquareSum(Temp)

    if Temp == 1:
        print(i, end  = '  ')
Python Program to Print Happy Numbers from 1 to 100

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.