HackerRank 30 days of coding – Day 9 ( Recursion)

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

 

 

Sample Input

3

Sample Output

6
#!/bin/python

import math
import os
import random
import re
import sys


def factorial(n):
       if n<0:
          return-1
       elif n<2:
          return1
       else:
          return n*factorial(n-1)

if __name__ == '__main__':
      fptr = open(os.environ['OUTPUT_PATH'], 'w')
      n = int(raw_input())
      result = factorial(n). 
      fptr.write(str(result) + '\n')
      fptr.close()

Leave a Reply