0

Does anyone know how could I change this code for it to to calculate the factorial of a number?

i = 0 while i < 5: print( i ) i += 1 else: print( "The loop ends, i is now", i ) print( "Done" )

26th Dec 2019, 8:39 PM
Esteban Rueda
Esteban Rueda - avatar
2 odpowiedzi
0
You want to actually save the accumulating values somewhere, say initialize product = 1 before the loop and then inside the loop have product *= i, then print product after the loop. Changing nothing else but that, your code would give you 4! I would use a for loop to save one step. And if you know about recursion, one would often approach the problem in that manner.
26th Dec 2019, 8:46 PM
Mark
Mark - avatar