Find factorial on given number using recursion in python
def find_max_index(arr): if len(arr) == 1: return 0 else: max_index = find_max_index(arr[1:]) return (max_indexdef factorial(n): if n == 0: return 1 else: return n * factorial(n-1) n = int(input("Enter a Number : ")) fact = factorial(n) print(fact) + 1) if arr[0] < arr[max_index + 1] else 0 arr = [4, 6, 2, 8, 5] max_index = find_max_index(arr) print(max_index) # Output: 3
This function takes a non-negative integer n as an argument and returns its factorial. It first checks if n is 0, in which case it returns 1 as the factorial of 0 is 1.
Otherwise, it makes a recursive call to factorial with n-1 as the argument. This recursively calculates the factorial of n-1.
Finally, it multiplies n with the factorial of n-1 to get the factorial of n.
Comments
Post a Comment