Please create a python program to find the factorial of a number by using while loop. Please answer in the simplest form and ASAP.
Answers
Solution:
The given problem is solved in Python.
n = int(input('Enter a positive integer: '))
if n < 0:
print('You must enter a positive integer.')
else:
factorial = 1
i = 1
while i <= n:
factorial = factorial * i
i = i + 1
print('Factorial of', n, 'is:', factorial)
Explanation:
Factorial of a positive number n is defined as the product of all the numbers from 1 to n.
Example:
To calculate factorial, we have assumed that factorial = 1. Now, we will start multiplying all numbers from 1 to n using while loop.
In the loop, we gave the condition the the numbers to be multiplied (i) must be less than the number. Now we will multiply the numbers inside the while loop and store the result.
At last, the factorial is displayed on the screen.
Refer to the attachment for output.
