Professional Documents
Culture Documents
Flow of Control
Flow of Control
CLASS: XI
SUB: Computer Science CODE: 083
Chapter : Flow of Control ( Looping based worksheet )
For Loop While Loop
1. Print number from 1 to 10 1. Print number from 1 to 10
2. Print even numbers from 1 to 100. 2. Print even numbers from 1 to 100.
3. Print odd numbers from 1 to 100. 3. Print odd numbers from 1 to 100.
4. Sum of first ten natural number. 4. Sum of first ten natural number.
5. Product of first 5 natural number. 5. Product of first 5 natural number.
6. Table of given Number 6. Table of given Number
7. Factorial of given number 7. Factorial of given number
8. Print reverse numbers 100 to 1 8. Print reverse numbers 100 to 1
Find the output Find the output Find the output Find the output
S=2 S=2 S=2 S=2
for I in range (3): I=1 for I in range (10,0,-2): I=20
S=S+I while(I<=3): S=S+I while(I>0):
print (I) S=S+I print (I, end=" ") S=S+I
print() I=I-2
Print(S)
print("Sum is",S) print("SUM IS",S)
Convert given loop in while loop: Convert given loop in for loop:
S=0 F=1
for I in range (2,11,2): I=1
S=S+I while(I<=5): F=F*I
print(S) Print(F)
Differentiate Between :[with example] Debug the following program
1.For and while s=1
2.Break , Continue For in range(2;50;3)
3.Infinite and Finite loop s%s+i
print(sum)
Q.1 what a range () function does? Give an example.
Q.2 what are loops in Python? How many types of loop are there in Python?
Q.3 WAP to print the following series –
(i) 1 4 7 10 . . . . . . ……….40
(ii) -1 -4 -7 -10 . . . . . . . .-40
(iii) 1-x+x2-x3+x4-……..xn
(iv) Calculate (1)+(1+2)+(1+2+3)+(1+2+3+n)
a 0 12345 ^ * @ #
a b 2 2 1234 ^ * @
a b c 4 4 4 123
a b c d 8 8 8 8 12
^ *
a b c d e 1 ^
1.Find the output 2.Find the output 3.Find the output 4.Find the output
S=2 S=2 S=2 S=2
for I in range (3): I=1 for I in range (10,0,-2): I=20
S=S+I while(I<=3): S=S+I while(I>0):
print (I) S=S+I print (I, end=" ") S=S+I
0 print() I=I-2
Print(S)
1 print("Sum is",S) print("SUM IS",S)
2 infinite 10 8 6 4 2
Sum is 32 SUM IS 112
Convert given loop in while loop: Convert given loop in for loop:
S=0 F=1
for I in range (2,11,2): I=1
S=S+I while(I<=50):
print(S) F=F*I
s=0 I+=5
I=2 Print(F)
while(i<11):
s=s+i F=1
i+=2 for I in range(1,51,5):
print(s) Print(F)
Differentiate Between : Debug the following program
1.For and while s=1 s=1
A for loop in Python is a control flow statement that is For in range(2;50;3) for in range(2,50,3):
used to repeatedly execute a group of statements as s%s+i s=s+i
long as the condition is satisfied. print(sum) print(s)
for x in range(2,7):
print(x) 2.Infinite and Finite loop- finite loop iterates for a finite number of
The while The while loop Python is used to iterate over iterations.
a block of code aslong as the test expression
(condition) is true. We generally use this loop when
we don't know the number of times to iterate
beforehand.
Infinite loop continues iterating indefinitely
while(x <4):
print x
x = x+1
Q.2 what are jump statements in Python? Name jump statements with example.
Python offers two jump statements to be used with in loops to jump out of loop-iterations.
These are break and continue statements.
Q.3 what are loops in Python? How many types of loop are there in Python?
Ans: Loops are iteration constructs in Python. Iteration means repetition of a set of statements depending upon a condition test.
Loops has three basic elements within it to repeat the statements –
Initialization (Start)
Check Condition (Stop)
Updation (Step)
Python provide two types of loop
(i) Conditional Loop while( (Condition based loop)
(ii) Counting loop for (loop for a given number of times)
Q.4 WAP to print the following series –
(i) 1 4 7 10 . . . . . . ……….40
1-x+x2-x3+x4-……..xn
(iii)
x=int(input("Enter value of x")) Enter value of x 2
Enter power of (n) 4
n=int(input("Enter power of (n)")) 1 1 -1
s,p=0,1 -2 -1 1
for i in range(n+1): 4 3 -1
t=(x**i)*p -8 -5 1
s+=t 16 11 -1
Sum of n terms 11
p*=-1
print(t,s,p) # additional command
print("Sum of n terms",s)
s='abcde'
for i in range(0,5):
for j in range(0,i+1):
print(s[j],end=" ")
print()
12345 n=s=5
1234 s=0
for i in range(n,0,-1):
123 for j in range(1,i+1):
12 print(j,end=" ")
1 s+=2
print()
^ * @ # s='^*@#'
^ * @ for i in range(4,0,-1):
for j in range(0,i):
^ * print(s[j],end=" ")
^ print()
Q.6 Write the following programs:
Find the sum of first n even and odd numbers.
n=int(input("Enter no.")) Enter no.10
even=odd=0 Sum of even no. 30
for i in range(n+1): Sum of odd no. 25
if i%2==0:
even+=i
else:
odd+=i
print("Sum of even no.",even)
print("Sum of odd no.",odd)