# Happy Number Program in Python using Recursion

Write a program using a recursive function in Python which takes one integer as function argument and checks whether the number is a Happy Number or not.

Definition: A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include a number less than 10.

Examples: 1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100, …

Take 97 → 92 + 72 = 130 → 12 + 32 + 02 = 10 → 12 + 02 = 1, so 97 is a Happy Number.

``````def isHappy(num):
sum = 0
while num != 0:
sum += (num % 10) ** 2
num //= 10
if sum == 1:
return True
elif sum < 10:
return False
else:
return isHappy(sum)

num = int(input("Enter the number: "))
if isHappy(num):
print(str(num) + " is a Happy Number!")
else:
print(str(num) + " is NOT a Happy Number!")``````

This site uses Akismet to reduce spam. Learn how your comment data is processed.