Untitled Document

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Chapter 6(Flow of control)

Question 1:
What is the difference between else and elif construct of if statement?

ANSWER:
‘Else’ is used along with ‘if’ to define the alternative path if the condition mentioned in the ‘if’
statement is incorrect. This means that only one statement can be evaluated using if..else
statement.

If more than one statements need to be evaluated, ‘elif’ construct is used. There is no limit
on the number of ‘elif’ construct which should be used in a statement. However, the
conditions mentioned in each ‘elif’ construct should be mutually exclusive i.e. if one of the
conditions in if..elif is correct it should imply that all the other conditions are false.

Question 2:
What is the purpose of range() function? Give one example.

ANSWER:
The range() function is a built-in function of Python. It is used to create a list containing a
sequence of integers from the given start value to stop value (excluding stop value). This is
often used in for loop for generating the sequence of numbers.

​ xample:
E
for num in range(0,5):
print (num)

OUTPUT:
0
1
2
3
​4

Question 3:
Differentiate between break and continue statements using examples.

ANSWER:
The 'break' statement alters the normal flow of execution as it terminates the current loop
and resumes execution of the statement following that loop.

Example:

num = 0
for num in range(5):
num = num + 1
if num == 3:
break
print('Num has value ' , num)
print('Encountered break!! Out of loop')

OUTPUT:

Num has value 1


Num has value 2
Encountered break!! Out of loop

When a 'continue' statement is encountered, the control skips the execution of remaining
statements inside the body of the loop for the current iteration and jumps to the beginning of
the loop for the next iteration. If the loop’s condition is still true, the loop is entered again,
else the control is transferred to the statement immediately following the loop.

#Prints values from 0 to 5 except 3


num = 0
for num in range(5):
num = num + 1
if num == 3:
continue
print('Num has value ' ,num)
print('End of loop')

OUTPUT:

Num has value 1


Num has value 2
Num has value 4
End of loop

Question 4:
What is an infinite loop? Give one example.

ANSWER:
The statement within the body of a loop must ensure that the test condition for the loop
eventually becomes false, otherwise, the loop will run infinitely. Hence, the loop which
doesn’t end is called an infinite loop. This leads to a logical error in the program.

Example:
num = 20
while num > 10:
print(num)
num = num + 1

​ he above statement will create an infinite loop as the value of ‘num’ initially is 20 and each
T
subsequent loop is increasing the value of num by 1, thus making the test condition num >
10 always true.
Question 5:
Find the output of the following program segments:

i) a = 110
while a > 100:
print(a)
a -= 2

ii) for i in range(20,30,2):


print(i)

iii) country = 'INDIA'


for i in country:
print (i)

iv) i = 0; sum = 0
while i < 9:
if i % 4 == 0:
sum = sum + i
i=i+2
print (sum)

v) for x in range(1,4):
for y in range(2,5):
if x * y > 10:
break
print (x * y)

vi) var = 7
while var > 0:
print ('Current variable value: ', var)
var = var -1
if var == 3:
break
else:
if var == 6:
var = var -1
continue
print ("Good bye!")

ANSWER:
i) The value of ‘a’ is decreased by 2 on each iteration of the loop and when it reaches 100,
the condition a >100 becomes false and the loop terminates.
OUTPUT:
110
108
106
104
102

ii) The range(start, stop, step) function will create a list from the start value to stop
value(excluding the stop value) with a difference of the step value.
The list thus created will be [20, 22, 24, 26, 28].
OUTPUT:
20
22
24
26
28

iii) The variable 'country' will act as a list of characters for the loop to iterate.
OUTPUT:
I
N
D
I
A

iv) The ‘while’ loop will run till the value of 'i' is 8, and the condition in ‘if’ function will return
true for two values i.e. 4 and 8, which will be added to ‘sum’. The last statement will print the
value of the variable ‘sum’.
OUTPUT:
12

v) The first loop will iterate over the list [1, 2, 3]


The second for loop will iterate over the list [2, 3, 4]
The print statement is printing the value of x * y i.e. [1*2, 1*3, 1*4, 2*2, 2*3, 2*4, 3*2, 3*3,
3*4] which comes out to be [2, 3, 4, 4, 6, 8, 6, 9, 12].
The break statement will terminate the loop once the value of x * y is greater than 10, hence,
the output value will be till 9.

Page No 140:

Question 1:
Write a program that takes the name and age of the user as input and displays a message
whether the user is eligible to apply for a driving license or not. (the eligible age is 18 years).

ANSWER:
Program:
#Program to check the eligibility for driving license
name = input("What is your name? ")
age = int(input("What is your age? "))
#Checking the age and displaying the message accordingly
if age >= 18:
print("You are eligible to apply for the driving license.")
else:
print("You are not eligible to apply for the driving license.")

OUTPUT:
What is your name? John
What is your age? 23
You are eligible to apply for the driving license.

Question 2:
Write a function to print the table of a given number. The number has to be entered by the
user.

ANSWER:
Program:
#Program to print the table of a number
num = int(input("Enter the number to display its table: "))
print("Table: ");
for a in range(1,11):
print(num," × ",a," = ",(num*a))

OUTPUT:
Enter the number to display its table: 5
Table:
5 × 1 = 5
5 × 2 = 10
5 × 3 = 15
5 × 4 = 20
5 × 5 = 25
5 × 6 = 30
5 × 7 = 35
5 × 8 = 40
5 × 9 = 45
5 × 10 = 50

Question 3:
Write a program that prints the minimum and maximum of five numbers entered by the user.

ANSWER:
Program:
#Program to input five numbers and print largest and smallest numbers
smallest = 0
largest = 0
for a in range(0,5):
x = int(input("Enter the number: "))
if a == 0:
smallest = largest = x
if(x < smallest):
smallest = x
if(x > largest):
largest = x
print("The smallest number is",smallest)
print("The largest number is ",largest)

Explanation of Program:
The first ‘if’ loop will assign the first value entered by the user to the smallest and largest
variable.

The subsequent if loop will check the next number entered and if it is smaller than the
previous value, it will be assigned to ‘smallest’ variable, and if greater than the previous
value it will be assigned to 'largest' variable.

After the end of the loop, the values of the ‘smallest’ and ‘largest’ variable are printed.

OUTPUT:
Enter the number: 10
Enter the number: 5
Enter the number: 22
Enter the number: 50
Enter the number: 7
The smallest number is 5
The largest number is 50

Question 4:
Write a program to check if the year entered by the user is a leap year or not.

ANSWER:
The year is a leap year if it is divisible by 4.

Program:
year = int(input("Enter the year: "))
if(year % 4 == 0):
print("The year",year," is a leap year.")
else:
print("The year",year," is not a leap year.")

OUTPUT:
Enter the year: 2020
The year 2020 is a leap year

Question 5:
Write a program to generate the sequence: –5, 10,–15, 20, –25..... upto n, where n is an
integer input by the user.

ANSWER:
The given sequence is: –5, 10, –15, 20, –20
The numbers in the sequence are multiples of 5 and each number at the odd place is
negative, while the number at even place is positive. The series can be generated by
checking each index for odd-even and if it is odd, it will be multiplied by '-1'.

Program:
num = int(input("Enter the number: "))
for a in range(1,num+1):
if(a%2 == 0):
print(a * 5, end=",")
else:
print(a * 5 * (-1),end=",")

The print(a*5*(-1)**a) statement can be also used inside 'for' loop as for even values of a,
(-1)a will return '1' and for odd, it will return (-1).

Program:
num = int(input("Enter the number: "))
for a in range(1,num+1):
print(a*5*(-1)**a, end=",")

OUTPUT:
Enter the number: 6
-5,10,-15,20,-25,30

Question 6:
Write a program to find the sum of 1+ 1/8 + 1/27 ...... 1/n3 , where n is the number input by
the user.

ANSWER:
Program:
sum = 0
#Getting the number input from user
n = int(input("Enter the number: "))
for a in range(1,n+1):
#calculating the sum
sum = sum + (1/pow(a,3))
#Sum of the series is
print("The sum of the series is: ",round(sum,2))

OUTPUT:
Enter the number: 5
The sum of the series is: 1.19
Question 7:
Write a program to find the sum of digits of an integer number, input by the user.

ANSWER:
The program can be written in two ways.
1. The number entered by the user can be converted to an integer and then by using
'modulus' and 'floor' operator it can be added digit by digit to a variable 'sum'.
2. The number is iterated as a string and just before adding it to 'sum' variable, the character
is converted to the integer data type.

Program 1:
#Program to find sum of digits of an integer number
#Initializing the sum to zero
sum = 0
#Getting user input
n = int(input("Enter the number: "))
# looping through each digit of the number
# Modulo by 10 will give the first digit and
# floor operator decreases the digit 1 by 1
while n > 0:
digit = n % 10
sum = sum + digit
n = n//10
# Printing the sum
print("The sum of digits of the number is",sum)

OUTPUT:
Enter the number: 23
The sum of digits of the number is 5

Program 2:
​#Initializing the sum to zero
sum = 0
#Asking the user for input and storing it as a string
n = input("Enter the number: ")
#looping through each digit of the string
#Converting it to int and then adding it to sum
for i in n:
sum = sum + int(i)

# Printing the sum


print("The sum of digits of the number is",sum)

OUTPUT:
Enter the number: 44
The sum of digits of the number is 8
Question 10:
Write a program to find the grade of a student when grades are allocated as given in the
table below. Percentage of the marks obtained by the student is input to the program.

Percentage of Marks Grade


Above 90% A
80% to 90% B
70% to 80% C
60% to 70% D
Below 60% E

ANSWER:
Program:
#Program to print the grade of the student
n = float(input('Enter the percentage of the student: '))

if(n > 90):


print("Grade A")
elif(n > 80):
print("Grade B")
elif(n > 70):
print("Grade C")
elif(n >= 60):
print("Grade D")
else:
print("Grade E")

OUTPUT:
Enter the percentage of the number: 60
Grade D

You might also like