In [1]:
number = int(input('Enter a number: '))factorial = 1if number < 0: print('Not Possible! Please Enter a Positive Number')elif number == 0: print('Factorial of 0 is 1.')else: for i in range(1, number + 1): factorial = factorial * iprint ('Factorial of: ', number, 'is: ', factorial)Enter a number: 7 Factorial of: 7 is: 5040
In [2]:
def factorial(num): #Defining the function if num < 0: return 'Not Possible' elif num == 0: return 1 elif num == 1: return 1 else: return num * factorial(num - 1)n = int(input('Enter a positive number: '))res = factorial(n)print('Factorial of: ', n, ' is: ', res)Enter a positive number: 6 Factorial of: 6 is: 720
No comments:
Post a Comment