You are on page 1of 4

Army Public School Bhopal

Date:05.09.2020 Class: XI [Worksheet 3]- IP – Loop Exercises

Q1. Find output of the following code:


a) z=0
x=6
y=0
while x > y :
z=z+x*y
x=x-1
y=y+1
print(x , y , z)
b) Term = 0
Incr = 1
while Incr <= 5 :
Incr = Incr + 1
Term = Term + Incr
print(Term)

Q2. How many times will the following loop execute? State the final values of variables num and x.
num = 0
x=6
while x > 1:
x=x–1
num += x

Q3. How many times following loop executes? Rewrite the following code using for loop.
a=2
while a < 50 :
print (a , ”\t”)
a += 3
print (“Quitting……”)
Q4. How many times the following loop executes? Predict the output.
a = 15
b=2
while a >= b :
b=b+2
a=a-3
print(a , “ @ “ , b)
Q5. Find the output of the following code:
a) x = 15
y = 10
z = 12
for a in range(1,5) :
if a== 1 or a==2 :
x=x+2
y=y-2
z=x+y
if a== 3 :
x=x+a
y=y-a
else :
z=z-a
x=x+2
y=y-a
print(“x=” , x , ” y= “ , y , “z= “, z)
b) x = 10
y = 15
z = 20
a=1
while a<=3 :
if a == 1:
x=x-a
y=y+a
elif a == 3 :
x=x+2
y=y-3
z=x+y
else :
z=z–2
x=x+1
y=y-1
print(x , ”:” , y , ”:” , z)
a=a+1

c) for a in range (5, 1, -2):


for b in range (1, a):
print (b, end=’\t’)
print ()

Q6. Convert following code using while loop without changing the output. Also state the output of the
following code:
a) sum = 0
for a in range (6, 0, -1):
sum = sum + a
print(sum)
b) for a in range (10, 0, -1):
if a%3 == 0 or a%5 == 0:
print(a)
Q7. Find the output of the following code and convert the code using while loop without changing the output.
a) x = 10
y = 15
z = 20
for a in range (5, 0, -2):
if a == 1:
x=x+1
y=y–1
z=x+y
elif a ==2:
x=x+2
y=x+3
z=x-y
elif a == 3:
x=x-1
y=y+1
z=z-2
else:
x=x-2
z=x+y
y=y-3
print (x, "#", y, ”#” , z)
b) Find the output of the following code and convert the code using for loop without changing the output.

x=5
y = 10
z = 15
a=1
while (a <= 4):
if a == 1:
x=x+1
y=y-1
z=x+y
elif a == 2:
x=x+2;
y=x+3;
elif a == 3:
x=x+1
z = x + y;
else :
x=x-2
z=x+y
y=y-3
a=a+1
print (z , "@" , y , ”@” , x)
Q8. Convert following code using for loop without changing the output. Also state the output of the following
code:
a) a=1
sum = 0
while a <= 3 :
sum = sum + a * a
print(sum)
a=a+1
b) a=1
sum = 0
b=5
while a <= b :
a=a+1
sum = sum + a * b
print(sum)
Q9 Predict the output of the following code and convert the code using while loop without changing the
output.
sum = 0
for x in range (1,6):
sum += x*x
print(sum)
print (x, sum, sep = "\t")
Q10 Predict the output of the code given below:
prod = 1
for a in [1,3,5,7]:
prod = prod * a
print(prod)
Q11 Predict the output of the following code.
a =1
b=100
while a<=b:
b = b//a
a *= 2
print(a, b)
Q12 Predict the output of the following code
x=1
y = 10
z=0
while x < y:
z += y * x
x=x+1
y=y-2
print (x, y, z, sep ="@")
print("OK")
Q13 Predict the output and state how many times does the loop execute and what id the final value of
variable count.
count=0
for i in range (2,15,3):
print(i*i)
count = count + i
Q14 Predict the output and state how many times does the loop execute.
sum = 0
for a in range (20, 5, -4):
print(a)
sum = sum + a
print(sum)
Q15 Predict the final values of variables a and b.
L = [15, 12, -1, 23, 100, 25]
a = b = L[0]
for i in L:
if i >= a:
a=i
if i <= b:
b=i
print ("a= ", a, "b = ", b)

Note: Show the progress of the code while calculating the output (Rough Part)

-----------------------

You might also like