factorial using recursion

     

 factorial using recursion






Recursive Algorithm :

module A(main function):
1.take the input from the user.
2.check and make sure it is a positive number.
3.pass the number to module B.
4.Print the value returned by module B as the required factorial.

module B(user defined function)
1.accept the value from calling module.
2.check if it is 1 , if yes return 1 to calling module.
3.decrement the value of the argument by 1.
4.pass it to module B.
5.get the value returned by module B and re-return it to calling module.

Program Code:

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


# take input from the user
num = input("Enter a number: ")

# check is the number is negative
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)





Share on Google Plus

About Vishnuprasad AS

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment