Tuesday, March 8, 2022

Hacker Rank 30 Day Challenge(Recursion-Factorial Program)

 Hacker Rank 30 Day Challenge(Recursion-Factorial Program)

Problem:

For a number n entered by the user, calculate its factorial. Use recursion to calculate.

Example:

n=4

Factorial of 4 is : 4x3x2x1

Factorial of 5 is: 5x4x3x2x1

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'factorial' function below.
#
# The function is expected to return an INTEGER.
# The function accepts INTEGER n as parameter.
#

def factorial(n):
i=1
r=1
for i in range(n):
r=r*(i+1)
i=i+1
return r


n=int(input().strip())

result = factorial(n)
print(result)

No comments:

Post a Comment