-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoops.py
More file actions
45 lines (29 loc) · 764 Bytes
/
Copy pathLoops.py
File metadata and controls
45 lines (29 loc) · 764 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Practice Question
# PQ 1: WAP to find the sum of first n number. (using while)
n = int(input("Enter number :"))
sum = 0
i = 1
while i<=n :
sum+=i
i+=1
print("sum using while loop =",sum)
# PQ 2: WAP to find the sum of first n number. (using for)
n = int(input("Enter number :"))
sum = 0
for i in range(n+1):
sum+=i
print("sum using for loop =",sum)
# PQ 3: WAP to find the factorial of first n numbers (using while)
n = int(input("Enter number :"))
fac = 1
i = 1
while i<=n:
fac*=i
i+=1
print("factorial using while loop =",fac)
# PQ 4: WAP to find the factorial of first n numbers (using for)
n = int(input("Enter number to find factorial :"))
fac =1
for i in range(1,n+1):
fac*=i
print("factorial using for loop =",fac)