14.3k views
5 votes
Write a recursive method called, doMyMath, which takes as input any positive integer and calculates the factorial (use comments for necessary items).

1 Answer

5 votes

Answer:

def doMyMath(n):

if n == 1:

return n

else:

return n*doMyMath(n-1)

Step-by-step explanation:

This line defines the function

def doMyMath(n):

This line checks if n is 1. If yes, it returns n (1)

if n == 1:

return n

If otherwise, it calculates the fibonacci recursively

else:

return n*doMyMath(n-1)

To call the function from main, you can use:

print(doMyMath(5))

or

num = int(input("Number: ")) --- This gets the number from user

print("Result: ",doMyMath(num)) --- This calculates the factorial

This question is answered in Python

User Dsdenes
by
4.5k points