+ 3

factorial using for loop

hey guys I made a factorial program using for loops and a list and it works so my question was is it better to use this or to use the recursion method coz I found this simpler and tell me if I have done a mistake anywhere. my code is : x = int (input (":")) y =1 list = list (range (1, (x+1))) for i in list: y*=I print (y)

31st Mar 2017, 12:14 PM
Tejas Bhatt
Tejas Bhatt - avatar
8 Answers
+ 2
I see and hey can u just tell me what does recursion exactly mean
31st Mar 2017, 12:23 PM
Tejas Bhatt
Tejas Bhatt - avatar
+ 2
i see
31st Mar 2017, 12:24 PM
Tejas Bhatt
Tejas Bhatt - avatar
+ 2
thanks alot
31st Mar 2017, 12:26 PM
Tejas Bhatt
Tejas Bhatt - avatar
+ 2
I tried both in py and SL seems to use more resource with recursion (you get bigger factorial with for loop before "time limit exceeded" msg) https://code.sololearn.com/cRR0883g759i/?ref=app https://code.sololearn.com/ciZe5KbLj3hT/?ref=app
24th Aug 2017, 10:09 AM
hamletmun
hamletmun - avatar
+ 1
Recursion is much easier for me def factorial(n): return n * factorial(n - 1) if n > 1 else 1 Num = 4 print("factorial of", Num, "is :", factorial(Num))
31st Mar 2017, 12:21 PM
Moataz El-Ibiary
Moataz El-Ibiary - avatar
0
It means function call itself under certain conditions
31st Mar 2017, 12:24 PM
Moataz El-Ibiary
Moataz El-Ibiary - avatar
0
nice
24th Aug 2017, 3:31 PM
Tejas Bhatt
Tejas Bhatt - avatar
0
You had placed 'I' in place of 'i' inside for loop But anyways, Good job ! Here is mine # Using Python https://code.sololearn.com/cM4VQXlTNKYF/?ref=app
17th Nov 2017, 9:36 AM
#RahulVerma
#RahulVerma - avatar