Write a program using functions to find out if a number is a Happy Number or not

Tech Programmer
0

WRITE A PROGRAM USING FUNCTIONS TO FIND OUT IF A NUMBER IS A HAPPY NUMBER OR NOT.



CODE FOR THE ABOVE  PYTHON  PROGRAM :




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 A HAPPY NUMBER")
    else:
        num = str(sum_sq_digits(num))
        ishappy(num)

num = input(" ENTER A NUMBER : ")
ishappy(num)



WRITTEN CODE FOR THE ABOVE PROGRAM :



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 A HAPPY NUMBER")

    else:

        num = str(sum_sq_digits(num))

        ishappy(num)

 

num = input(" ENTER A NUMBER : ")

ishappy(num)



OUTPUT :




Post a Comment

0Comments
Post a Comment (0)

Featured Highlighted Categories

You'll discover all of the most up-to-date bring innovative here.

Python List theory with List Programs
Get complete python lists theory and list basic and insane programs for your practice.
CBSE SAMPLE PAPERS
Get CBSE sample and question papers of last 7 years with answers to get best practice for your Board Exams
Python String theory and String Programs
Python strings complete theory with illustrations and practice programs for free