Recursion In Python

 

DIFFERENT TYPES OF RECURSION PROGRAMS IN PYTHON :

  • PYTHON PROGRAM TO CALCULATE FACTORIAL OF A NUMBER USING RECURSION



1) CALCULATE FACTORIAL OF A NUMBER USING RECURSION


Q  WRITE A PYTHON PROGRAM TO CLACULATE FACTORIAL OF A NUMBER USING RECURSION


CODE FOR THE ABOVE  PYTHON  PROGRAM :



def recur_factorial(n):
    if n ==1:
        return n
    else:
        return n*recur_factorial(n-1)

num = int(input(" Enter any no  : "))
if num<0:
    print("sorry, factorial does not exist for negative numbers")
elif num ==0:
    print("the factorial of 0 is 1 ")
else:
    print("the factorial of "num"is"recur_factorial(num))


WRITTEN CODE FOR THE ABOVE PROGRAM :


def recur_factorial(n):

    if n ==1:

        return n

    else:

        return n*recur_factorial(n-1)


num = int(input(" Enter any no  : "))

if num<0:

    print("sorry, factorial does not exist for negative numbers")

elif num ==0:

    print("the factorial of 0 is 1 ")

else:

    print("the factorial of ", num, "is", recur_factorial(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