Welcome folks today in this blog post we will be finding factorial
of a number using for
and while
loop in python. All the full source code of the application is shown below.
Get Started
In order to get started just make an app.py
file inside the root directory and first of all
Using For Loop
We will be using the for loop to find factorial of number
app.py
1 2 3 4 5 6 7 8 |
num = int(input("enter a number: ")) fac = 1 for i in range(1, num + 1): fac = fac * i print("factorial of ", num, " is ", fac) |
While Loop
And now we will be finding factorial of a number using while
loop
app.py
1 2 3 4 5 6 7 8 9 10 |
num = int(input("enter a number: ")) fac = 1 i = 1 while i <= num: fac = fac * i i = i + 1 print("factorial of ", num, " is ", fac) |
And now if we execute the python
script by typing the below command
python app.py