Q. Rewrite following code fragments using while loops:


(a)

min = 0

max = num

if num < 0 :

    min = num

    max = 0 # compute sum of integers from min to max

    for i in range (min, max + 1) :

        sum += i


(b)

for i in range(1, 16) :

    if i % 3 == 0 :

        print (i)


(c)

for i in range(4) :

    for j in range(5) :

        if i +1 == j or j + i == 4 :

            print ("+", end = " ")

    else :

        print ("o", end = " ")

print ()



Answer =


#(a)
min = 0
max = num
if num < 0 :
    min = num
    max = 0 # compute sum of integers from min to max
    i = min
    while i < max + 1 :
      sum += i
      i += 1


#(b)

i = 1
while i < 16 :
  if i % 3 == 0 :
    print (i)
  i += 1


#(c)

i = 0
j = 0
while i < 4 :
    j = 0
    while j < 5 :
        if i + 1 == j or j + i == 4 :
            print("+",end=" ")
        j += 1
    else :
        print("o",end = " " )
    i += 1
print()

9 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post