Loops2 Assignment Apc
Loops2 Assignment Apc
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:
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
(a for a in range(5):
1
print(a)
2
# 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
b=b-1
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:
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