Q. A happy number is a number in which the eventual sum of the square of the digits of the number is equal to 1.
Example : 12 = (1)2+(2)2 =1+4=5
Hence , 12 is not a happy number
Example 100 = (1)2 + ( 0 )2 + ( 0 )2 = 1+ 0 + 0 = 1
Hence , 100 is a happy number.
Write a program that takes a number and checks if it is a happy number by using following two functions in it :
Sum_sq_digits :returns the sum of the square of the digits of the number x, using the recursive technique
Ishappy() : checks if the given number is a happy number by calling the function sum_sq_digits and displays an appropriate message
Answer =
You can understand by Watching video :-
def sum_sq_digits (num,n= 0):
if n == len(num) :
return 0
else :
return ( int ( num [ n ] ) ) ** 2 + sum_sq_digits (num,n+1)
def Ishappy( num ):
if sum_sq_digits (num) == 1 :
print("Happy number ")
elif len(num) == 1 :
print("Not happy number ")
else :
num = str( sum_sq_digits (num) )
Ishappy( num )
num = input("Enter a number : ")
Ishappy( num )
Output :-
Enter a number : 100
Happy number
>>>
Enter a number : 64
Not happy number
>>>
Enter a number : 68
Happy number
>>>
Enter a number : 28
Happy number
>>>
Enter a number : 40
Not happy number
>>>
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )