I wrote a code so that I can calculate factorials using loop methods in python. But when I runned my program(provided below) i behaves like an infinite loop. Help me to get it out.
a = int(input('n...'))
while a >= 0:
a *= a - 1
print(a)
The problem in your code is that you are updating the same variable a
while check if its greater than 0 in while loop condition. If a
is multiplied by any non-zero number, your code would always enter an infinite loop. Following is how you can correct it:
a = int(input('n...'))
factorial = 1
while a > 0:
factorial *= a
a -= 1
print(f'The factorial is: {factorial}')