Python Program to check if a number is Happy number
What is a Happy number?
A number is said to be a happy number if it results in 1 when replaced by the sum of squares of its digits recursively. If this process results in an endless loop of numbers containing 4, then the number will be an unhappy number.
Example:
100 is a happy number
100
1^2 + 0^2 + 0^2 = 1
Python Program to check if a number is a Happy number
def isHappy(n):
r = s = 0;
while(n > 0):
r = n%10
s += r**2
n //= 10
return s
n = int(input())
res = n;
while(res != 1 and res != 4):
res = isHappy(res)
if(res == 1):
print("happy number")
elif(res == 4):
print("not a happy number")