Sunday 6 April 2014

How to find factorial of a number using Python in both iteration and recursive method

#!/usr/local/bin/python2.7
# pleases follow the indention as it is most important in python. python don't use begin/end, if/end if or loop/
#end loop so python identify statement by indention.

def Factorial(n):
    fact=1
    for i in range(1,n+1):
       fact=fact*i

    return fact


def FactorialRecursive(n):
  if n <=1:
    return 1
  return n*FactorialRecursive(n-1)

n = input ('Enter the number to find factorial: ')
print('\nFactorial of a number %d is %d'%(n,Factorial(n)))

print('Factorial (recursive) of a number %d is %d'%(n,FactorialRecursive(n)))

No comments: