Show the output of the following programs. (Hint: Draw a table and list the variables in the columns to trace these programs.)
(a)
for i in range(1, 5):
j = 0
while j < i:
print(j, end = " ")
j += 1
(b)
i = 0
while i < 5:
for j in range(i, 1, -1):
print(j, end = " ")
print("****")
i += 1
(c)
i = 5
while i >= 1:
num = 1
for j in range(1, i + 1):
print(num, end = "xxx")
num *= 2
print()
i -= 1
(d)
i = 1
while i <= 5:
num = 1
for j in range(1, i + 1):
print(num, end = "G")
num += 2
print()
i += 1
index | progarm | output |
(a) | for i in range(1, 5): j = 0 while j < i: print(j, end = " ") j += 1 |
0 0 1 0 1 2 0 1 2 3 |
(b) | i = 0 while i < 5: for j in range(i, 1, -1): print(j, end = " ") print("****") i += 1 |
**** **** 2 **** 3 2 **** 4 3 2 **** |
(c) | i = 5 while i >= 1: num = 1 for j in range(1, i + 1): print(num, end = "xxx") num *= 2 print() i -= 1 |
1xxx2xxx4xxx8xxx16xxx 1xxx2xxx4xxx8xxx 1xxx2xxx4xxx 1xxx2xxx 1xxx |
(d) | i = 1 while i <= 5: num = 1 for j in range(1, i + 1): print(num, end = "G") num += 2 print() i += 1 |
1G 1G3G 1G3G5G 1G3G5G7G 1G3G5G7G9G |