0
Write a function that calculates the factorial of any integer number.
i am trying to solve this problem in python and this is my first day with python , I Know how to solve it with java & cpp, but i got a wrong answer with python https://onlinegdb.com/KaSO5IH12
4 Answers
+ 4
Please save the attempt in SL Playground and provide the link here.
+ 3
#The return should be after while loop:
def factorial(n):
p=1
i=1
while i<=n:
p *= i
i += 1
return p
print(factorial(int(input())))
0
Check out recursion in python core course
https://code.sololearn.com/cvM25c0HGWxt/?ref=app
0
You can solve this in one of two ways: with recursion, or with loops. Unless I'm mistaken though, the question wants you to do it with recursion.
Well, the factorial of a number n is the product of whole numbers from n to 1. So, when you define your recursive function, it has to be something like this:
def func(n=1):
if(n > 1):
return func(n - 1) * n
return 1
DISCLAIMER: haven't tested this, since I'm only doing this from memory.