CBSE Class 10 Computer Applications Question 14 of 26

Python Conditionals and Loops — Question 5

Back to all questions
5
Question

Question 5(a)

Rewrite the following code fragments using for loop

while num > 0 :
    print num % 10 
    num = num/10
Answer
l = [1]
for x in l:
    l.append(x + 1)
    if num <= 0:
        break
    print (num % 10)
    num = num/10