Write a function that calculates the factorial of any integer number. | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
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

2nd Aug 2021, 7:02 PM
Mahmoud Hamed
Mahmoud Hamed - avatar
4 Réponses
+ 4
Please save the attempt in SL Playground and provide the link here.
2nd Aug 2021, 7:31 PM
JaScript
JaScript - avatar
+ 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())))
2nd Aug 2021, 7:41 PM
JaScript
JaScript - avatar
0
Check out recursion in python core course https://code.sololearn.com/cvM25c0HGWxt/?ref=app
2nd Aug 2021, 7:50 PM
Allan 🔥STORMER🔥🔥🔥🔥
Allan 🔥STORMER🔥🔥🔥🔥 - avatar
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.
2nd Aug 2021, 8:13 PM
BootInk
BootInk - avatar