LAB3 Loop
LAB3 Loop
Experiment 1.1
1. Fill in the blanks
Source code Output
############ 1.py ########### &
k=0 1
while k < 5 : 2
print(k) 3
k += 1 &
print(k) S
print(k) 3
print(k) 2
&
for k in range(0,5,1) :
ว
print(k)
3
print(k)
&
while k < 5 : 2
print(k) 3
if k == 3 : break 3
k += 1
print(k)
ธิ
############ 8.py ###########
2
k=1
&
while k < 5 :
k += 1
·
if k == 3 : continue
print(k)
print(k)
print(k)
print(k)
n if/else i j Output 4 if · % 8
- - 2 5 - 5 else &
S 8
0 if 4 5 4 6 else 8 9 &
1 if 6 5 6 7 if | · (8
2 elSe 6 6 6 8 else 1 1 Ig
3. (Count positive and negative numbers and compute the average of numbers)
Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and
computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a
floating-point number. Here is a sample run:
Your Source code:
4. (Conversion from kilograms to pounds) Write a program that displays the following
table (note that 1 kilogram is 2.2 pounds):
Kilograms Pounds
1 2.2
3 6.6
...
197 433.4
199 437.8
1 &: 15 16
2 print (' Kilograms', " It's Pounds',',' l',' Pounds',' It's 17
3 Kilograms' ( 18
4 for king in range ( 1,200, 2) : 19
5 a + = 5 20
6 print ( kg, ' it',' it',' . If '% ( kg * 2.2) , ' t, ' l: 21
7 a. It's It','%. If '%. ( a * 0.45359231)) 22
8 23
9 24
10 25
11 26
12 27
13 28
14 29
15 30
5. (Financial application: compute future tuition) Suppose that the tuition for a university is $10,000 this year and increases 5% every
year. Write a program that computes the tuition in ten years and the total cost of four years’ worth of tuition starting ten years
from now.
1 P = 10000 16
2 t= 17
3 While t! = 15: 18
4 += 1 19
5 if t = =10 : 20
6 =++ 3 / 100 21
7 A = P *( 1+r* t) 22
8 print (' Tuition in ten years is:' , Al 23
9 if t > 10 : 24
10 r = + + 5 1 100 25
11 B = P *( 1 + t)
r * 26
12 B + = B 27
13 if t = =14 : 28
14 print (' The total cost of four years' worth of 29
15 tuition starting ten years from now is: ', A + B) 30
6. (Display a pyramid) Write a program that prompts the user to enter an integer from 1 to 15 and displays a pyramid, as shown in
the following sample run:
Hint: https://www.youtube.com/watch?v=VKJNLp5ZKb4&ab_channel=Amulya%27sAcademy
1 a= 0 11
2 for in range ( 1,99.2) :
in 12
3 b = 1( 2)
n n + 13
4 a + = D 14
5 print ('". If '". ( a) 15
6 16
7 17
8 18
9 19
10 20
Hint: an = 2n − 1
8. (Compute ) You can approximate π by using the following series: For i = 10000 pi = ……………………
1 1 1 1 1 ( −1)i +1
π = 4 1 − + − + − + + For i = 20000 pi = ……………………
3 5 7 9 11 2i − 1
For i = 100000 pi = ………………….
Write a program that displays the value for i = 10000, 20000, and 100000.
1 =0 11
2 i = eval( input (' Enter of pi:', 3) 12
3 for in range ( 2,1+
in 1) : 13
4 b = (- 1) ** ( 1) / ( 2 n-
*
R+ 1
14
5 a + = b
15
6 print (' for its i, pi is: ", a*4) 16
7 17
8 18
9 19
10 20