You are on page 1of 6

DELHI PUBLIC SCHOOL, AZAAD NAGAR, KANPUR

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)

Q.4 WAP to print the following pattern

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 ^

Q.5 Write the following programs:


 Find the sum of first n even and odd numbers.
 To check whether square root of a given number is prime or not.
 Fibonacci series 0,1,1,2,3,5,8,13……n
 To check whether no. is Armstrong no. or not
 To check whether no. is Palindrome or not
 To print 1 to 10 tables by using nested loop
DELHI PUBLIC SCHOOL, AZAAD NAGAR, KANPUR
CLASS: XI
SUB: Computer Science CODE: 083
Chapter : Flow of Control ( Looping based worksheet solution )
For Loop While Loop
1. Print number from 1 to 10 1. Print number from 1 to 10
for i in range(1,11): i=1
print(i) while(i<=10):
2. Print even numbers from 1 to 100. print(i)
for i in range(2,101,2): i=i+1
print(i) 2. Print even numbers from 1 to 100.
3. Print odd numbers from 1 to 100 i=2
for i in range(1,101,2): while(i<=100):
print(i) print(i)
i=i+2
4.Sum of first ten natural number. 3. Print odd numbers from 1 to 100.
sum=0 i=1
for i in range(1,11): while(i<=100):
sum=sum+i print(i)
print(sum) i=i+2

5.Product of first 5 natural number. 4. Sum of first ten natural number.


f=1 sum=0
for i in range(1,6): i=1
f=f*i while(i<=10):
print(f sum=sum+i
) print(sum)
5. Product of first 5 natural number(Factorial of 5).
6. Table of given Number f=1
n=int(input("enter any Number")) i=1
for i in range (1,10): while(i<=5):
print(n*i) f=f*i
print(f)
7. Factorial of given number
n=int(input("enter any Number")) 6. Table of given Number
f=1 n=int(input("enter any Number")) i=1
for i in range (1,n+1): while(i<=10):
f=f*i print(n*i)
print ("factorial is",f) i=i+1
7. Factorial of given number
8. Print reverse numbers 100 to 1 n=int(input("enter any Number")) f=1
for i in range(100,0,-1): i=1
Print(i) while(i<=n):
f=f*i
print ("factorial is",f)
9. Print reverse numbers 100 to 1
I=100
while(i>0):
print(i)
i-=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.1 what a range () function does? Give an example.


The range () function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a
specified number. its syntax is range (start, stop, step) e.g.
x = range (3, 6)
for n in x:
print(n)
O/P
345

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

(ii) -1 -4 -7 -10 . . . . . . . .-40


for i in range(-1,-41,-3):
print(i)

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)

(iv) Calculate (1)+(1+2)+(1+2+3)+(1+2+3+n)


n=int(input("Enter no. of terms"))
for i in range(2,n+1): Enter no. of terms5
s=t=0 Term 1 : 1
for j in range(1,i): Term 2 : 3
t+=j Term 3 : 6
print("Term",(i-1),":",t) Term 4 : 10
s+=t sum of 5 term is 10
print("sum of",n,"term is",s)

Q.5 WAP to print the following pattern


a 0
ab 2 2
abc 4 4 4
abcd 8 8 8 8
abcde

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)

 To check whether square root of a given number is prime or not.

 Fibonacci series 0,1,1,2,3,5,8,13……n


t=int(input("Enter the no. of terms"))
f,s=0,1 Enter the no. of terms8
print("fibonacci series\n",f,s,end=" ") fibonacci series
for i in range(2,t): 0 1 1 2 3 5 8 13
n=f+s
print(n,end=" ")
f=s
s=n
 To check whether no. is Armstrong no. or not
#153,370,371,407 armstrong no's
Enter a 3 digit no.153
n=int(input("Enter a 3 digit no."))
153 is Armstrong no.
s,t=0,n
>>>
while(t>0):
Enter a 3 digit no.110
digit=t%10
110 is not Armstrong no.
s+=digit**3
t//=10
if(n==s):
print(n,"is Armstrong no.")
else:
print(n,"is not Armstrong no.")

 To check whether no. is Palindrome or not


#121,34543,343,131,48984 are palindrome no's. Enter a 3 digit no.121
n=int(input("Enter no.")) 121 is a pallindrome no.
s,t=0,n >>>
while(t>0): Enter a 3 digit no.123
digit=t%10 123 is not a pallindrome no.
s=s*10+digit
t//=10
if(n==s):
print(n,"is a pallindrome no.")
else:
print(n,"is not a pallindrome no.")
 To print 1 to 10 tables by using nested loop
n=int(input("Enter no.")) Enter no.5
for i in range(1,11): #rows 1 2 3 4 5
for j in range(1,n+1): #columns 2 4 6 8 10
print("{:3d}".format(i*j),end=" ") 3 6 9 12 15
4 8 12 16 20
print()
5 10 15 20 25
6 12 18 24 30
7 14 21 28 35
8 16 24 32 40
9 18 27 36 45
10 20 30 40 50

You might also like