KEMBAR78
Loops Assignment Apc | PDF | Control Flow | Software Development
0% found this document useful (0 votes)
11 views6 pages

Loops Assignment Apc

Uploaded by

mehar77puri
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)
11 views6 pages

Loops Assignment Apc

Uploaded by

mehar77puri
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/ 6

Review Exercises

1. Analyse the given code and answer the following questions:


for iin range(3, 5):
for jin range(2, i):
print("Python")
(i) How many times does the inner loop
(ii) Write the output of the execute?
Ans. (i) 3 times program segment
(ii) Python
Python
Python
2. Study the following code and answer
for i in range(3): the given questions:
for jin range(2):
n=j**2
print(n, end=' )
(a) What will be the total number
of iterations?
(b) Predict the output.
Ans. (a) The loop executes 6
timnes.
(b) 010101
3. What will be the final value
of ans when the following
ans=0 nested loop is executed?
for iin range(4):
for jin range(5):
ans=ans+1
print(ans)
Ans. The final value of ans when
the iterations are completed: 12
4. Predict the output of the
given code:
for iin range(4):
for jin range(3):
print("**, end= ")
print( )
Ans. Output:

5. Rewrite the following code


for i in range(5, 0, -1):
snippet by using nested while loops:
for jin range(1, 6):
print(i, end=' ')
print( )
Ans. i=5
while(i>=l): Output:
j=1 5555 5
while(j<=5): 44444
print(i, end=' ) 3333 3
j=j+1 22222
print( ) 1111 1

370
Chapter 10 -Iterative
Assertion
and Reason based questions: 371 Computatlons
) Assertion (A): When two or more loops are used in a code, they are known as nested
6
loops.

(R): The nested loop is used in such a way that the inrer
Reason

for each
iteration of the outer loop. loop repeats a number
assertion and reasoning,
times
of the above
on pick arn appropriate
Based given below:
options statement from the
(a) Both A and R are true and Ris the correct explanation of A.
(b) Both A and R are true and Ris not the correct explanation of A.
false
is true but R is
(c) A
is false but Ris true.
(d) A false.
are
(e) Both A and R
is false but R is true.
(d) A
() Assertion (A):
Ans. The nested loop can also be designed using while loops.
Reason (R): In this situation, a while loop is placed inside another while loop and the inner
number of times for each iteration of the outer loop.
loop repeats a
Based on the above assertion and reasonirng, pick an appropriate statement from the
options given below:
a) Both A and R are true and R is the correct explanation of A.
explanation of A.
6) Both A and R are true and R is not the correct
false.
(c) A is true but R is
(d) A is false but R is true.
(e) Both AandR are false.
correct explanation of A.
Ans, (a) Both A and R are true and R is the
7. Case Study based questions:
teacher has given a code to generate
As a homework assignment, your There are some places in the code
right side.
a pattern as shown alongand ?4? to be filled with statements/expressions
marked with ?1?, ?2?, ?3? as shown below:
code should execute properly. The code is
so that the
Homework assignment

# To create a pattern
i=5
while(i>=l):
j=?1?
while(?2?):
print("*end=")

?3?
print( )
questions:
?4? following
answer the
code, place of ?1?
With reference to the above
s t a t e m e n t / e x p r e s s i o n in
place of ?2?
(1) What will be the statement/expression in place of
?3?
will be the in
(ii) What statement/expression
place of ?4?
(iüi) What will be the statement/expression in

(iv) What will be the


Ans. (i) i
(ü) j >= 1
(i) j= j -1
(iv) i= i-1
Exercises
I. Multiple Choice
Questions (Tick () the correct answers):
1. Which of the following
(a) The break statement statements is not valid for a nested loop?
can be used to terminate
(6) The outer loop can be inner as well as outer loop.
(c) The inner loop can be terminated from the block of inner loop.
(d) Inner loop repeats the terminated from the block of outer loop.
loop. execution a number of times for each iteration of the o
2. Which of the
following punctuation marks is put
(a) colon at the last of loop
3. Which
(b) comma
(c) semicolon statement?
of the following loop is not (d) None of these
(b) nested whilesupported by the Python language?
(a) nested for
4. Given is a nested (c) nested do-while
loop construct: (d) None of these
for i in range (1,4):
for jin range (1,i):

Statements
print( )
How many
(a) 2
times the above nested
(b) 3
loop will iterate?
5. A program (c) 4
s= 10 srippet using nested (d) 1
loop construct is
for iin given below:
range(1, 4):
for jin range (1,
i)
S t= j
print(s)
What will be the
(a) 11 final value of 's'
(b) 12 when the above
II. Fill in the
blanks: (c) 13 program snippet is
1.A loop within (d) 14 executed?
2. You can another loop is called a
use the
3. The
Or the
statement to
terminate a loop block.
iump from either
the inner loop
4. Repetition of the or the outer statements can be used to create a
loop.
5. The .loop takes
some statements of the statemernt will repeat aplace before the
loop. loop for next loop.
III. Write whether the iteration after ignoring
following statements are
1. The loop is a repetitive
structure. True/False:
2, The nesting of loops means
3. When break statement is
restricting the use of
used, inner
terminate the loop.
it will
program completely.
372
4. The outer loop Chapter 10-Iterative Computations 373
moves for the next iteration
completely. once the inner loop has
5. The continue iterated
statement can be used to run only
the inner parts of a
nested loop.
TV. Assertion and Reason
based questions:
1. Assertion (A): The
break statement is used for the
the nested loops. unusual termination of a block in
Reason (R): This statement can be
used in the outer as well as in
means, as soon as the break statement
is executed, the control willthe inner loop. lt
block byignoring the rest of the comne out of the
Based on the above assertion anditerations of the loop.
options given below: reasoning, pick an appropriate statement rom
the
(a) Both A and R are true and
R is the correct explanation
(b) Both A and R are true and R is of A.
not the correct explanation of A.
(c) Ais true butR is false.
(d) A is false but R is true.
(e) Both Aand R are false.
2. Assertion (A: The continue statement
cannot be used in the nested loops.
Reason (R): If the continue statement is used in the
return for the next iteration of the outer loop. And, outer loop block, then it will not
the code will develop an infinite
loop.
(a) Both A and R are true and R is the correct
explanation of A.
(b) Both A and R are true and R is not the correct
explanation of A.
(c) A is true but R is false.
(d) A is false but R is true.
(e) Both A and R are false.
V. Case Study based questions:
1. A number is said to be a prime number if it has only two factors. Given is a code to
generate the prime numbers from 1 to 100:
# A code to display all prime numbers from 1 to 100
print("Prime numbers 1-100:")
for iin range (1,................): # statement 1
C=0
for jin range(1,......... # statement 2
if i%j==0:
C=C+1
if c==,.......: # statement 3
print(........ # statement 4
print('Program Over!")
There are some places left blank to be filled with the values/expressions. Write
appropriate value/expression to be filled in the blank space of:
(i) Statement 1 (iü) Statement 2 (i) Statement 3 (iv) Statement 4
2. Factorial is defined as the product of all positive numbers from 1 to the given number.
Samunder has designed a code using nested while loop to generate the factorial of
first five numbers. He has left some places blank where he could not complete the
statement. The code is as illustrated below:
#A code to display factorial of a number
a=l
while(a<=5):
374 Textbook of Computer Sclence with Python

n=int(input("Enter a number:")
f........ # statement 1
b=l
while(b<=,..... ): # statement 2
f=......... # statement 3
b=b+1
print("Factorial ofn'............ # statement 4
a=a+1
As a good programmer, help him to fill in the blanks with
expression in: appropriate vvariable/
i) Statement 1 (ii) Statement 2 (ii) Statement 3 (iv) Statement 4
VI. Predict the output of the following codes, when
executed:
1. for iin range(5):
for j in range(i,-1,-1):
printj, end= )
print( )
2. for x in range(1, 4):
for y in range(1, 3):
print(x*y, end=' )
print( )
3. for a in range(7, 1, -2):
for b in range(1, 3):
print(a*b, end= ")
print( )
4. for x in range(1, 7):
for y in range(1, x-1):
if(x==3):
break
print(y, end=' ")
print( )
5. i=1
while(i<=5):
j=5
while(j>=1):
print(j, end= )
jj-1
print( )
isj+1
VIL. Answer the following questions:
1. What do you mean by a nested loop?
2. In which situation do you need to use
a nested loop?
3. Write down a construct by using the break'
statement in an outer loop.
4. Write down the syntax of using
the nested loop.
5. Distinguish between 'break' and 'continue'
statements.
Chapte toeratve Computatterns
codes
VIL. Unsolved Python
1.Write aPython code to display the code for máthemaieat tables from 5 to f0. Each
mathematical table should tun for 10 interatiohs (4s shown in the given format)
Sample Output: Table of 5
5*1 5
52 =10

5*10 = 50
2. Write a Python code to accept any 20 numbers and display only prime numbers.
[Hint: A number is said to be a prime number if it is divisible by I and the number
itself.]
3. Write a program code to compute and display the sum of the following series:
1+2 1 +2+ 3 1+2 + 3 +
S= 1 *2*3*
1 *2 1*2*3
of its
4. A number is said to be a Happy number' if the eventual sum of the square
digits results in 1. Write a Python code to input a number and check whether it
is a
Happy Number or not.
Sample Input: 31
Sample Output: 31=32+1²=10=1+0=1
Hence, it is a Happy Number.
series:
5. Write progranm codes in Python to find the sun of the given
1 1 1 1
(a) 1 +,t + n!
3! 4!
+ (1+2+3+ +n)
(b) 1 + (1+2) + (1+2+3) +
(c) 1 + (12) + (12*3) + + (1*2*3*. .n)
1 1 1
(d) 1+ 1+2
+
1+2+3 1+2 + 3....

6. Write the program codes in Python to display the following patterns:


(a) 5 4 3 2 1 (b) 1 3 5 79
4 3 2 135 7
5 4 3 13 5
5 13
1
5
(d)1 2 3 4 5
(c) 5 2 3 4 5
5
3 4 5
5 4
5 4 3 2 4 5
5
5 4 3 2 1
(e) 9 9 9 9 9 () 9
79
77 7 7 7
5 5 5 5 5 5 7 9
3 5 7 9
33 3 3 3
1 11 1 1
13 5 7 9
(g) 9 (h) 1
9 7 23
9 7 5 4 5 6
9 75 3 7 89 10
975 3 1 11 12 13 14 15

You might also like