Home >>Python Programs >Python program to check if the given number is Happy Number
In this example, we will see a Python program through which we can check if a given number is a Happy number or not.
Any given number is said to be a happy number if it yields 1 when replaced by the sum of squares of its digits repeatedly. If this process results in an endless cycle of numbers containing 4 then that number will be said an unhappy number.
#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;
num = 35;
result = num;
while(result != 1 and result != 4):
result = isHappyNumber(result);
#Happy number always ends with 1
if(result == 1):
print(str(num) + " is a happy number");
#Unhappy number ends in a cycle of repeating numbers which contain 4
elif(result == 4):
print(str(num) + " is not a happy number");