Q. Rewrite the following code fragments using for loop:


(a)

i = 10

while (i > 0) :

    print (i)

    i -= 3


(b)

while num > 0 :

    print (num % 10)

    num = num / 10


(c)

while (num > 0) :

    count += 1

    sum += num

        num -= 2

        if count == 10 :

            print ( sum / float(count))

            break



Answer =

(a)
for i in range (10,0,-3):
    print (i)

(b)
for i in range (n):
    print (n % 10)
    n = n / 10
    if n < 0 :
        break

(c)
for i in range (num,0,-2):
    print(num)
    count += 1
    sum += num
    if count == 10 :
        print ( sum / float(count))
        break

2 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