0% found this document useful (0 votes)
6 views

LAB3 Loop

หห่ห่หน

Uploaded by

thitaporn3003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

LAB3 Loop

หห่ห่หน

Uploaded by

thitaporn3003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

College of Industrial Technology

King Mongkut's University of Technology North Bangkok


030513900 Computer Programming
Experiment 3 : Loop
Name : น.ส. ตา พร ดวงพร ม section & No.

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

############ 2.py ########### S


k=5
&
while k > 0 :
3
print(k)
2
k -= 1
&
print(k) ·

############ 3.py ###########


k=5 S
while True : &
print(k) 3
k -= 1 2
print(k) ....

############ 4.py ########### S


for k in range(5,0,-1) : &

print(k) 3

print(k) 2

&

############ 5.py ########### &

for k in range(0,5,1) :

print(k)
3
print(k)
&

############ 6.py ###########


1
for k in range(1,10,3) :
&
print(k) %
print(k) &

############ 7.py ###########


k=1 I

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)

############ 9.py ###########


for k in range(1,10,2) :
if k > 6 :
break :

print(k)
print(k)

2. From the code below fill out the table.


i=2
j=5
for n in range(0,10) :
if i < j :
i += 2; print(i)
else :
j += 1; print(j)

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 else 6 & 9 els e (O 1 ||

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:

1 ล: O 21 print (' The overage is, num/ ca- 1))


2 nure = 0 22
3 D=- 1 23
4 C: 0 24
5 b=อ 25
6 While b! = 0; 26
7 b = eval( input (' Enter an integer, the input ends ( 27
8 if it is 0:13 28
9 if b 20 and b! = 0 : 29
10 2 + = 1 30
11 elif b!= 0 : 31
12 &+ = 1 32
13 num + = 33
14 a = a +1 34
15 if a = = 1 : 35
16 print ( " You didn't enter any number" ( 36
17 if a:: 1 : 37
18 print (' The number of positives is', bl 38
19 print (' The number of negatives is cl 39
20 print (' The total is', num) 40

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

Your Source code:

1 print (' Kilograms', ' It',' Pounds') 11


2 for king in range ( 1,200,2): 12
3 print ( kg, ltlt',' o. If ' ( kg * 2.23 13
4 14
5 15
6 16
7 17
8 18
9 19
10 20

(Conversion from kilograms to pounds and pounds to kilograms)


Write a program that displays the following two tables side by side (note that 1 kilogram is 2.2 pounds and that 1 pound is .45
kilograms):

Kilograms Pounds | Pounds Kilograms


1 2.2 | 20 9.09
3 6.6 | 25 11.36
...
197 433.4 | 510 231.82
199 437.8 | 515 235.09
Your Source code:

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.

Your Source code:

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:

Your Source code:

1 h = int( input ( enter the number of rows:')) 9 print()


2 fori range (
in 1, n+1) : 10
3 for; in range ( 1, 1):
n- i + 11
4 print (",", end = ") 12
5 13
for; in range ( i, 0, - 1) :
6 14
print (' 1.3. Of '%. ( 3), end = "
7 15
for; in range ( 2,1 + 1 ) :
8 16
print ('% 3. Of ' . (3), end = ")

Hint: https://www.youtube.com/watch?v=VKJNLp5ZKb4&ab_channel=Amulya%27sAcademy

7. (Sum a series) Write a program to sum the following series:


1 3 5 7 9 11 95 97
+ + + + + + + +
3 5 7 9 11 13 97 99

Your Source code:

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.

Your Source code:

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

Name : น.ส. ตา พร ตาง พรม section % No.


ธิ

You might also like