SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
.loops
Exercise:
Check Point
Question:11 | ISBN:978013274719 | Edition: 6

Question

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

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

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

 

0 0

Discussions

Post the discussion to improve the above solution.