Python Day 9: Recursion 3




Write a factorial function that takes a positive integer, N as a parameter and prints the result of N! (N factorial).

Note: If you fail to use recursion or fail to name your recursive function factorial or Factorial, you will get a score of 0.



  1. #!/bin/python3
  2. import math
  3. import os
  4. import random
  5. import re
  6. import sys
  7. # Complete the factorial function below.
  8. def factorial(n):
  9. for i in range (1, n):
  10. n= n*i
  11. return n
  12. if __name__ == '__main__':
  13. fptr = open(os.environ['OUTPUT_PATH'], 'w')
  14. n = int(input())
  15. result = factorial(n)
  16. fptr.write(str(result) + '\n')
  17. fptr.close()




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube