0% found this document useful (0 votes)
37 views12 pages

Loops2 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)
37 views12 pages

Loops2 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/ 12

Review ENercises

and their usee


jump statements
1. Name two statements are:
jump
Ans. The two control exits the loop block as soon as break
Break statement: The
(1)
executed.
statement is
(ii) Continue statement: The control goes back for the next iteration of the
skipping the rest of the statements of the loop. loop by
2. What will be the output of the following code?
m,n=2, 15
for i in range(1, 5):
m=m+1

n=-1
print("'mn ="m)
print("'n = "n)
Ans. m =6
n =11
3. Analyse the following program segment and determine how many times the loop will. be
executed? What will be the output of the program segment?
k=1
/=2
while(i<6):
k=ki
i=i+1
print(k)
Ans. The loop will execute 4 times and the output will be 120.
4. State the differences between fixed and unfixed iterative loops.
Ans. The differences between fixed iteration and unfixed iterative loops are:

Fixed Iteration Unixed Iteration


number of iterations in a
1. The looping construct used to repeat the 1. When the
execution of a block of statements for a loop is uncertain, it is referred to as an
fixed number of times is known as the unfixed iterative loop.
fixed iterative loop.
is an unfixed iterative
2. The for loop is an example of fixed 2. The while loop
iterative loop. loop.
5. Convert the given snippet using while loop.
f=1
for i in range(1, 6):
f=i
print()
Ans. f, i=1,1
while(i<=5):
f=i
j=i+1
print(f)
used in a loop?
6. What is the difference between a break and a continue statement the break statement

Ans. A break statement is used for unusual termination of a loop. As soon as u n e x e c u t e d .

is executed, the control exits the loop leaving the remaining iterations

344
if(i==5): Computations 345
break
print(i)
When the
the current continue statement is executed,
For example: iteration unexecuted. the control
returns for the next
for i in range(1, iteration leaving
11):
if(i==5):
continue
print(i)
7. What is an
Ans. Aloop that infinite loop? Give an
never ends its example.
example iterations is
of an infinite loop is: said to be an
i=l infinite loop or an endless loop. An
while(i<5):
print()
(8.)Write the output of the
X = 97 following code snippet:
while(x<=100):
print(x)
if(x%10=-0):
break
X=X+1
Ans. The output: 97
98
99
100
Rewrite the following code snippet by using the while loop and give the output.
p=100
for iin range(1, 5):
p=p-1
print(p, end=' ')
Ans. Code snippet using the while construct:
p=100
a=1
while(a<5):
p=p-1
print(p, end=' ")
a=a+1
The output is: 99 98 97 96
using the for loop and give the output.
10. Rewrite the following code snippet by
pz-99
while(p<0):
p-p+9
print(p, end=' )
Ans. p=99
for p in range(-99, 0, 9):
p=p+9
print(p, end=') -45 -36 -27 -18 -9 0
-90-81 -72 -63 -54
The Output is:
346 Textbook of Computer Science with Python

11. Assertion and Reason based questions:


(i) Assertion (A): An accumulator is a variable that gets updated with a value
during each
iteration of a loop.
Reason (R): It is a must to initialise an accumulator to avoid getting garbage value
the execution of the program. during
Based on the above assertion and reasoning, pick an appropriate statement from
options given below:
the
(a) Both Aand Rare true and Ris the correct explanation of A.
(b) Both Aand 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.
Ans, (a) Both A and R are true andR is the correct explanation of A.
(i1) Assertion (A): A loop is a non-repetitive structure in which a statement or a set of
statements are executed until the desired number of iterations is over.
Reason (R):The fixed iterative loop is a looping construct used to repeat the execution of
a block of statements for a fixed number of times.
Based on the above assertion and reasoring, pick an appropriate statement from the
options given below:
(a) Both A and R are true and R is the correct explanation of A.
(b) Both Aand R are true and R is not the correct explanation of A.
(c) Ais true but R is false.
(d) A is false but R is true.
(e) Both A and R are false.
Ans. (d) A is false but R is true.
12. Case Study based questions:
() Anita is facing difficulties in predicting the output for some of the code snippets while
going through her textbook. Help her to sort out her problem. Predict the missing outputs
below:
(in the right column) for the snippets (written in left column of the table) shown
Snippet Output

(a for a in range(5):
1
print(a)
2

(b) for a in range(5, 0):


print(a)

(c) for a in range(-10, 0, 2):


print(a)

(d) for iin "PYTHON":


print(i)
(c) -10 (d) P
Ans. (a) 0 (b) Not output Y
-8
2
4 H
3
4 -2
N
(6) Given beloW 1S
The a
integers
will be are rython code to find
entered terminated
accepted and added the sum of Chapter 10-Iteratve Computations
as 'N. and integers based on a
In the the sum will betogether untHl
347

values/variables: given below code, displaved onthethechoce i6 entered user


ch=..........# statement 1 there are the screen, as soon ascontroleoo
. The io0P
sum=0 some blank places as the cho
to be filed
while (ch==Y); with

# statement 2
Sum=Sum+.
ch=input("Want
print("Sum of the
to
# statemment 3
continue.. Enter
numbers:",..... Y/N")
print("Program
With reference
Over!") ...) # statement 4 ch
to
(a) What will be the above code, answer
evel
the
(b) What will be the value/variable to be illedfollwing questions:
in
(c) What will be the value/variable to be filled statement 1?
the value/variable in
(d) What will be to be filled in statement 2?
the value/variable to
be filled in
statement 3?
Ans. (a) Y" statement 4?
(b) int(input("Enter a number")
(c) n
(d) sumn
Exercises
1. Multiple Choice Questions (Tick ( )the correct answers):
1. When execution of the statements in a progtam are repeated several
then the construct used is known as: times sequentially,
(a) execution (b) loop (c) selection (d) none of these
2. Which of the following statements is a fixed iterative loop?
(a) for (b) while (c) if (d) if-else
3. Which of the following loops does not
execute even once, if the condition is falss sk
the beginning?
(a) infinite (b) while (c) for (d) none of these
4. To execute a loop ten times, which of the
following statements will be used?
(a) for iin range(6, 26, 2): (b) for iin range(3, 30, 3):
(c) for i in range(11): (d) all of these
5. Which of the following statements holds valid with 'for'
loop?
(a) initial value is optional (b) update value is vital
(c) stop value is an optional (d) none of these
6. Which of the following loops need not check the
(a) User controlled loop
condition before execution begins?
(b) for loop
(c) while loop (d) all of these
7. To find the sum of the whole numbers up
to 10, a loop runs:
(a) once (b) ten times
(c) eleven times
(d) any number of times
8. How many times will the following loop run?
Given: pz-5
for iin range(p, 20, 5):
(a) 3 times (b) 0times (c) infinite times (d) 4 times
9. With the help of which looping construct, an infinite loop can be
created?
(a) Fixed iterative loop (b) while
(c) for (d) all of these
10. A loop statement is given as:
for iin range(10):
How many times will the loop execute?
(a) 0 time (b) 9 times (c) 10 times (d) infinite times
IIL State whether the following statements are True or False:
1. The 'while' loop gets executed at least once.
2. The break' statement allows us to exit a loop.
3. While loop can be considered as entry controlled loop.
4. The 'continue' and break' statements are used for the same
purpOses.
5. When a program uses two 'for' loops, they are known as infinite loops.
6. We cannot write a loop that can execute forever.
7. In a loop, the if-else statement results in either True or False.
8. In an if statement, the multiple conditions can be joined with logical operatoS

348
Chapter tÛ - tterative Computattons
9, A loop that never ends is
called as an infinite loop.
10. The control can move for the next itetatin t he tors using
contínue statemet

II. Predict the output of the following code


snippets:
1. for iin range-1, 10): 2.
i=2; k=1
print(i+1) while(i<6):
k'=i
i=i+1
print(k)

3.m=2; n=15 4. a=81; b=3


for i in range(1, 5): while(a>=b):
m=m+1 a=a/b
n=n-1 print(a)
print('m ="m)
print('n ="n)
6.
5.
x=5; y=50 p=0
for i in range(1, 5):
while(x<=y):
print(i)
y=y//x
P=p+1
print(y)
print(p)
8 a=20; b=0
7. a=1
while(a>b):
while(a<=10):
print(a*a)
print("Python")
a=a-5
a=a+2

10. m2; b=11


9.a=5; b=l
for a in range(m, m+10, 2):
while(a>):
if(b>0):
if b>0:
print(m)
print("Positive")
else:
else: print(m*m)
print("Negative")
b=b-5
a=a-1

b=b-1

based questions: for


IV. Assertion and Reason function is a built-in function
which is used with the
rangel )
1, Assertion (A): The
parameter. It is used in
for loop to
loop. function does not require any numbers,
Reason (R): This numbers, positive and negative
such as even and odd
generate the series
etc.
350 Textbook of Computer Science with Python

Based on the above assertion and reasoning, pick an appropriate statement frow i
options given below:
(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) Ais false but R is true.
(e) Both A and R are false.
2, Assertion (A): In Python, a fixed iterative loop worksfor a given condition.
Reason (R): The unfixed iterative loop is used when the user is unaware of ha
number of times the process is to be repeated. Here, a set of statemernts is executed
repeatedly till a given condition is true. As soon as the given condition false, the
loop wil be terminated.
Based on the above assertion and reasoning, pick an appropriate statement from the
options given below:
(a) Both A and R are true arnd 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.
3. Assertion (A): The break and continue statements are known as the jump statements.
Reason (R): They are used in a looping construct to terrinate the loop if the given
condition is true.
Based on the above assertion and reasoning, pick an appropriate statement from the
options given below:
(a) Both A and Rare 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 butR is true.
(e) Both A and R are false.
4. Assertion (A): An infinite loop can be created with the help of a for loop.
be created
Reason (R): This type of looping construct wil never come to an end. It can
by omitting one or more parameters of the loop.
Based on the above assertion and reasoning, pick an appropriate statement from the
options given below:
(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.
while loop and vice-versa.
5. Assertion (A): A for loop can be converted into a
converted to another form,
Reason (R): When one form of repetitive structure is
keeping the model of program logic the same is
known as inter-conversion of the
looping structure.
statement from the
Based on the above assertion and reasoning, pick an appropriate
options given below:
(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.
Chapter 10- Iterative Computations 351
Case Study based questions:
1. After learning the looping concept, Sagar has designed a
ten integers using jor loop. Furtlher, he wants to code to find the sum of ay
rewrite the same code usin8
loop but due to some confusion, he couldn't complete it. The code using for looP ana
the incomplete code usirng while loop are written in fwo diferent columns of a table
shown below:

Code using for loop Code using while loop


# To find the sum of any ten
numbers # To find the sum of any ten numbers
s=0
S= ?1?
for iin range (10): i=1
n=int(input("Enter a number:") while(i<=10):
S=S+n ?2?
print("Sum of the numbers:"s) ?3?
print("Program Over!") ?4?
print("Sum of the numbers:"s)
print("Program Over!")
statements/values to
Help him to complete the program by providing appropriate
be filled in the spaces marked with:
(iüi) ?3? (iv) ?4?
(i) ?1? (ü) ?2?
digits of a
design a code in Python to reverse the
2. Your younger brother is trying to arnd hence, he has left them
number. He is unable to write some
of the statements
blank marked with ?1?, ?2?, ?3? and ?4?.
number
#A program to reverse the
r=0
n=int(input("Enter a number : ")
while(n!=0):
?1?
?2?
23?
print("Reversed Number:", ?42)
code:
questions to complete his
Answer the followings t a t e m e n t / v a r i a b l e in place of ?1?
(i) What will be the statement/variable in place of ?2?
(ii) What will be the statement/variable in place of ?3?
the
(ii) What will be statement/variable in place of ?4?
the
(iv)What will be
'while' loops:
the following code snippets using
VI. Rewrite
1. n=191; c=0
for i in range(1, n):
if(n%i==0):
C=c+1

if(c==2):
print("Prime")

else:
print("Not Prime")
352 Textbook of Computer Science with Python
2. n=5
for iin range(n):
ifi==0):
print("Zero")
else:
print('Square of"i,"is",i)
3. for i in range(2, 20, 3):
if(i%2==0):
print(i, 2*i)
else:
print(i, 3*i)
VII. Rewrite the following code snippets using the 'for loop:
1, n=l; a=l
while(n<=5):
print(a*a--1)
a=a+1

n=n+1
2. import math
n=l;a=1i
while(n<-5):
print(math.pow(2,n)-1)
n=n+1
3. a=l; s-0
while(a<=10):
S=Sta

a=a+1

if(a==5):
continue
print(s)
4. a=1; $=0
while(a<=20):
n=int(input("Enter a number")
if(a==5):
break
S=S+n

a=a+2

print(s)
VIII. Answer the following questions:
1. Explain the 'for' loop with an example.
2. Name the different types of looping
constructs.
3. What are the parameters needed to create a 'for
loop?
4. Name two jump Chapter 10 - Iterative Computations
5. A'while statements. 353

loop
6. Write down is an entry restricted
the general loop. Justify.
(a) for loop format of a:
7. What is the (b)while loop
(a) break purpose of the following
8. What is statements in a loop?
meant by a user (b)continue
9. What is the controlled loop?
10. Define the significance of pass statement in a loop?
following:
(a) Finite loop Explain.
(c) Infinite loop (b) Delay loop
11. State one similarity
and one diference (d) Null loop
12, Differentiate
between step loop and between 'while' and 'for loops.
X. Unsolved Python Codes: continuous loop.
1. Write the codes in
Python to display the first ten terms of
(a) 1, 4, 9, 16, the following series:
(b) 1, 2, 4, 7, 11,
(c) 0, 3, 8, 15,
(d) 4, 8, 16, 32,
(e) 2, 5, 10, 17,
2. Write a Python code to input any 50
Perform the following tasks: numbers (including positive and negative).
(a) Count and display the positive numbers
(b) Count and display the negative numbers
(c) Display the sum of positive numbers
(d) Display the sum of negative numbers
3. Write a Python code to input a range of numbers from m to n (where, m <n). Calculate
and display the sum of all odd numbers and even numbers in the range of numbers
from m to n (both inclusive).
4. Write a Python code to display all the numbers between mand n (where man, m>0,
n>0) which are perfect squares.
For example: 25, 36, 49, are said to be perfect square numbers.
5. Design Python codes to find and display the sum of the following series:
(a) 1 + 4 + 9 + + 400

(b) (1 + 2) + (1 + 3) + + (1 + 20)
(c) (1 + 3) + (1 + 5) + + (1+ 19)
- 20
(d) 2- 4 +6- 8 +
+ (19*20)
(e) (1-2) + (2*3) +
6. Write a Python code to check and display whether a number input by the user is a
Composite number or not.
and the
[A number is said to be composite, it it has one or more factors excluding 1
number itself.]
For example: 4, 6, 8, 9,
sum of the following series:
7. Write Pvthon codes to find and display the
+ an
(a) S = a + a²+ a +
354 Textbook of Computer Science with Python
a
(b) S = 3- + +

1
2n -1
1 1
(c) S =-+ a2 + + 1
an
a a
(d) S= +
n
1 2 3
(e)=
S a an

8. Write a Python code to input anumber and find the smallest digit of the input number
Sample Input: 6524
Sample Output: Smallest digit is 2
9. Write a Python code to input a numnber and check whether it is a prime number
or not. If it is not a prime number then display the next number which is a prime
number.
Sample Input: 14
Sample Output: 17
10. A special two-digit number is a number such that when the sum of the digits of
the number is added to the product of its digits, the result is equal to the original
two-digit number.
For example: Consider the number 59.
Sum of digits = 5 +9 = 14
Product of digits =5*9 = 45
Sum of the sum of digits and product of digits = 14 + 45 = 59
whether it is
Write a Python code toaccept a two-digit numnber. Check and display
a special two-digit number or not.
display whether it is a Niven
11. Write a Python code to input a number. Check and
Number' or not.
by the sum of its digits.)
(A number is said to be Niven when it is divisible
Sample Input: 126
divisible by 9.
Sum of its digits = 1+2+6 =9 and 126 is
Number' or
number and check whether it is a 'Spy
12. Write a Python code to accepta
not.
digits equals the product of its digits.)
(A number said to be a Spy if the sum of its
Sample Input: 1124
Sum of the digits = 1 + 1 +2+4= 8
Product of the digits = 1*1*2*4 =8
number. Display the sum of each
digit,raised to the
13. Write a Python code to input a
digits.
power of the number of
Sample Input: 64
Sample Output: Number of digits =
2
Result = 62 + 42= 36 + 16 = 52
Sample Input: 425
Sample Output: Number of digits = 3
Result = 43 + 23 + 53 = 64 + 8 + 125 = 197 and display
from the user. Check
14. Write a menu driven Python code to accept ta number based on
user's choice.
whether it is a perfect number or an automorphic number factors including 1 and

(a) Perfect number: Anumber is called perfect, if sum of its


excluding the number itself is equal to the original number.
For exanple: 6 = 1+ 2 +3
Chapter 10 -IterativeComputations 355
(b) Automorphic number: An
in the last digit(s) automorphic number is the number which is contained
of its square.
For example: 25 is an
as the last two digits. automorphic number as its square is 625 and 25 is present
15. Write a menu driven
Python code to perform the
choice: following tasks as per the user's
(a) To display the series:
0,3, 8, 15, 24, *. to n terms.
) To find and display
the sum of the series:
1 5
S=746 to n terms

You might also like