Skip to content

Commit 3523e2f

Browse files
authored
Create Method1(While loop)
Calculating the factorial of a number using a while loop
0 parents commit 3523e2f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Method1(While loop)

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Method 1(While loop)
2+
3+
# Defining function that calculates the factorial of number 'n'
4+
def factorial1(n):
5+
if n == 0:
6+
return 1
7+
8+
i = 0
9+
f = 1
10+
11+
while i < n:
12+
i = i + 1
13+
f = f * i
14+
15+
return f
16+
17+
18+
# Asking user for a number to initiate calculation
19+
x = int(input('Calculate factorial of: '))
20+
21+
# Printing the factorial of the user's input
22+
print(factorial1(x))

0 commit comments

Comments
 (0)