How to make a factorial? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How to make a factorial?

n=int(input("number : ")) sum=0 while n: print(n, "*") if n==0: break print(sum)

16th Apr 2018, 1:54 AM
park
2 Answers
+ 5
Here are a few suggestions for your code - The first would be to a condition in your while statement that replaces the if statement within it, setting it so that the while statement will only run while n is greater than 0: while n > 0: statements Next, as the factorial of a number is that number multiplied by every other number below it, I would set a second variable to be equal to one (in this case sum will work as that variable), so you can multiply n by something as it decreases. To find the actual calculations of the factorial of n, I would recommend setting sum (which should equal 1 initially) to equal the value of sum multiplied by n, having n decrease by one after every iteration of the loop: while n > 0: sum *= n n -= 1 Hope this helped! d:
16th Apr 2018, 2:03 AM
Faisal
Faisal - avatar
+ 3
wow good ideas!! thank you so much Faisal and Jan Markus!! have a great day :):)
16th Apr 2018, 2:10 AM
park