Friday, 5 July 2019

12th-Prog-01 Write a Python Programme for Recursively find the factorial of a natural number.


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


#main programme
n=int(input("enter n"))
if n==0:
    print("factorial is 1")
elif n<0:
    print("Sorry factorial does not exist ")
else:
    print("factorial is:",r_fact(n))

Output
enter n 5
('factorial is:', 120)

enter n -3
Sorry factorial does not exist

enter n 0
factorial is 1



No comments:

Post a Comment