0% found this document useful (0 votes)
16 views7 pages

CH 6 FLOW OF CONTROL TB Exercise & Extra Questions

The document contains exercises on flow control in Python, including outputs of various code segments, conversions between for and while loops, and error corrections. It also presents questions regarding loop execution counts and expected outputs for given code snippets. Additionally, it includes a section on calculating factorials and handling user inputs.

Uploaded by

ai.crimson158
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views7 pages

CH 6 FLOW OF CONTROL TB Exercise & Extra Questions

The document contains exercises on flow control in Python, including outputs of various code segments, conversions between for and while loops, and error corrections. It also presents questions regarding loop execution counts and expected outputs for given code snippets. Additionally, it includes a section on calculating factorials and handling user inputs.

Uploaded by

ai.crimson158
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter-6 FLOW OF CONTROL -TB EXERCISE

Q. Find the output of the following program segments:


(i) a = 110
while a > 100:
print(a)
a -= 2
Ans: 110
108
106
104
102
(ii) for i in range(20,30,2):
print(i)
Ans: 20
22
24
26
28
(iii) country = 'INDIA'
for i in country:
print (i)
Ans: I
N
D
I
A
(iv) i = 0; sum = 0
while i < 9:
if i % 4 == 0:
sum = sum + i
i=i+2
print (sum)
Ans: 12
(v) for x in range(1,4):
for y in range(2,5):
if x * y > 10:
break
print (x * y)
Ans: 2
3
4
4
6
8
6
9

(vi) var = 7
while var > 0:
print ('Current variable value: ', var)
var = var -1
if var == 3:
break
else:
if var == 6:
var = var -1
continue
print ("Good bye!")
Ans: Current variable value:7
Current variable value:5
Good bye!
Current variable value:4

IMPORTANT QUESTIONS
How many times is the following loop executed?
1. i=100
while i<=200:
print(i)
i+=20
Ans : 6 times
2. for a in range(100,10,-10)
print(a)
Ans: 9 times
3. i=0
while i<0 and i>2:
print(“Hello..”)
i=i+1
Ans: 0 times
4. x=45
while x<50:
print(x)
Ans: Infinite times
5. n=25
while n>10:
print(n*2)
n-=3
Ans: 5 times

Convert the following for loop into while loop


i=1
for i in range(1,100): while i<100:
if i%4==2: if i%4==2:
1
print(i) print(i)
i=i+1

a=25
for a in range (25,500,25): while a<500:
2 print(a) print(a)
a=a+25

a=90
for a in range(90,9,-9):
while a>9:
3 print(a)
print(a)
a=a-9
c=0
c=0 d=2
for d in range(2,10,2): while d<10:
print(c*d) print(c*d)
4
c+=1 c+=1
print(“Series Over”) d+=2
print(“Series over”)

Convert the following while loop into for loop


i=100 for i in range (100,0,-3):
while i > 0: print ( i )
1 print (i )
i- = 3

i=0 for i in range (100):


while i < 100: If i % 2 = 0:
if i % 2 = 0: print(i, “is even”)
2 print(i, ”is even”) else:
else: print(i, “is odd”)
print(i, “is odd”)
i=i+1
i = 10 for i in range(10,250,50):
while i<250: print(i)
3 print(i)
i = i+50

i=88 for i in range(88,8,-8):


while i>8: print(i)
4 print(i)
i-=8

ERROR CORRECTION
Rewrite the following code in Python after removing all syntax
error(s).Underline each correction done in the code.
countdown=10 countdown=10
while countdown>0 while countdown>0:
print countdown print (countdown)
1
countdown-1 countdown=countdown-1
print(“Finally”) print(“Finally”)

Limit=40 Limit=40
for M in range[0,Limit]: for M in range(0,Limit):
if M%5=0: if M%5==0:
2 print(M*2) print(M*2)
else else:
print(m+2) print(M+2)

250=Number Number=250
WHILE Number<=1000: while Number<=1000:
if Number=>750: if Number>=750:
print Number print (Number)
3 Number=Number+100 Number=Number+100
else else:
print Number*2 print (Number*2)
Number=Number+50 Number=Number+50
30=To To=30
for k in range(0,To) for k in range(0,To):
IF k%4==0: if k%4==0:
print(k*4) print(k*4)
4
Else: else:
print(k+3) print(k+3)

Find the output of the following Python code:


S No. Code Output

1 x=10 10 0
y=0 91
while x>y: 82
print(x,y) 73
x=x-1 64
y=y+1
2 if the input is 6: 1
n=int(input(“Enter an integer:”)) 4
if n<1: 9
print(“Invalid value”) 16
else: 25
for i in range(1,n+1): 36
print(i*i)
3 x,y=5,20 5 20 4
while x<y: 8 17 9
if y%x==0: 11 14 3
z=y//x
else:
z=y-x
print(x,y,z)
x+=3
y-=3
4 sum,step=0,5 45 @ 20
for i in range(0,6,2):
step+=5
sum+=step
print(sum,”@”,step)
5 for a in “abcde”: a+b+c+d+e
print(a,’+’,end=’ ‘)
6 for i in range(0,10): 9
pass
print(i)

7 for i in range(10,1): No output


print(“Hello”)
8 for a in range(2,7): #
for b in range(1,a): ##
print(‘#’,end=’ ‘) ###
print() ####
#####
9 for a in range(2,7): 1
for b in range(1,a): 12
print(b, end=” “) 123
print() 1234
12345
10 while(6+2>8): Going out
print(“GotCha!”)
else:
print(“Going out!”)

11 N=int(input(“Enter N:”)) a. 6
i=1 b. 0
sum=0
while i<N:
if i%2==0:
sum=sum+i
i=i+1
print(sum)

a. What is the output when the input value is 5?


b. What is the output when the input value is 0?
12 Consider the following Python program, intended to calculate a. Program will
factorials: execute
infinitely.
number=int(input(“Enter number”)) b. while n>0:
n,result=number,1 Or
while True or n: while n:
result=result*n
n=n-1
factorial=result
print(“factorial of”,number,”is”,factorial)

a. What happens when the user enters a value of 5?


b. How would you fix this program?
13 count=0 Hello
while count<10: Hello
print(“Hello”) Hello
count+=1 Hello
Hello
Hello
Hello
Hello
Hello
Hello

14 keepgoing= True 100


x=100 90
while keepgoing: 80
print(x) 70
x=x-10 60
if x<50: 50
keepgoing = False
15 c=0 5
for x in range(10): 10
for y in range(5): 15
c+=1 20
print(c) 25
30
35
40
45
50
16 for i in range(4): O+O+O+O+O++
for j in range(5):
if i+1==j or j+1==4:
print(“+”,end=” “)
else:
print(“o”,end=” “)

**********************

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy