# NCERT Solution Flow of Control in Python Class 11 Chapter 6

Share with others

## Summary of Chapter

```The if statement is used for selection or decision making.

The looping constructs while and for allow sections of code to be executed repeatedly under some condition.

for statement iterates over a range of values or a sequence.

The statements within the body of for loop are executed till the range of values is exhausted.

The statements within the body of a while are executed over and over until the condition of the
while is false.

If the condition of the while loop is initially false, the body is not executed even once.

The statements within the body of the while loop must ensure that the condition eventually becomes
false; otherwise, the loop will become an infinite loop, leading to a logical error in the program.

The break statement immediately exits a loop, skipping the rest of the loopâ€™s body. Execution continues
with the statement immediately following the body of  the loop.

When a continue statement is encountered, the control jumps to the beginning of the loop for the
next iteration.

A loop contained within another loop is called a nested loop.```

## Q1. What is the difference between else and elif construct of if statement?

Ans. if..else statement allows us to write two alternative paths and the control condition determines which path gets executed. If the condition is incorrect/False then else statement will execute. The syntax for if..else statement is as follows.

`if condition:       statement(s)else:       statement(s)`

Many a times there are situations that require multiple conditions to be checked and it may lead to many alternatives. In such cases we can make a chain of conditions using elif. Syntax of elif is shown below:

```if condition :
statement
elif condition :
statement
elif condition :
statement
|
|
|
|

else :
statement```

## Q2. What is the purpose of range() function? Give one example.

Ans. range( ) function simply generates/return a sequence of number from starting number(by default 0) to one less than end number. This function is commonly used in for loop. for example

```for i in range(3, 7):
print(i)```

OUTPUT :

3

4

5

6

## Q3. Differentiate between break and continue statements using examples.

Ans. break statement is used for immediate termination of loop for example

`for i in range(7):     if i==3:          break     print(i)print("Loop Terminate")`

OUTPUT :

0
1
2
Loop Terminate

continue statement is used to skip the current iteration of the loop and pass the control to the next iteration. for example :

`for i in range(7):     if i==3:          continue     print(i)print("Loop Terminate")`

OUTPUT :

0
1
2
4
5
6
Loop Terminate

## Q4. What is an infinite loop? Give one example.

Ans. A never ending loop is called infinite loop. for example the following loop will print ‘A’ infinite times

`i=0while(i<=0):     print("A")`

## Q5. Find the output of the following program segments:

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

Ans.

110
108
106
104
102

`b) for i in range(20, 30, 2):         print(i)`

Ans.

20
22
24
26
28

`c) country = 'INDIA'     for i in country:          print (i)`

Ans.

I
N
D
I
A

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

Ans. 12

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

Ans.

2
3
4
4
6
8
6
9

`f) 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!")`

## Flow of Control in Python Class 11

PROGRAMMING EXERCISES

## Q1. 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).

```Ans.

nm = input("Enter your name")
age = int(input("Enter your age"))
if age >= 18:
print("You are eligible for driving licence")
else:
print("You are not eligible for driving licence")```

## Q2. Write a function to print the table of a given number. The number has to be entered by the user.

```Ans.

def table(n):
for i in range(1, 11):
print("7 * ", i," =", n*i)

num = int(input("Enter any number"))
table(num)

OUTPUT : (If number entered by user is 7)

7 *  1  = 7
7 *  2  = 14
7 *  3  = 21
7 *  4  = 28
7 *  5  = 35
7 *  6  = 42
7 *  7  = 49
7 *  8  = 56
7 *  9  = 63
7 *  10  = 70```

## Q3. Write a program that prints minimum and maximum of five numbers entered by the user.

```Ans.

num1 = int(input("Enter any number"))
max=num1
min=num1
for i in range(4):
num = int(input("Enter any number"))
if max < num:     max = num   if min > num:
min=num
print("Minimum number is :",min)
print("Maximum number is :",max)```

## Q4. Write a program to check if the year entered by the user is a leap year or not.

```Ans.

yr = int(input("Enter any year"))
if yr%100 == 0:
if yr%400 == 0:
print("Leap Year")
else:
print("Not a Leap Year")
elif yr%4 == 0:
print("Leap Year")
else:
print("Not a Leap Year")```

## Q5. Write a program to generate the sequence: â€“5, 10, â€“15, 20, â€“25â€¦.. upto n, where n is an integer input by the user.

```Ans.

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

## Q6. Write a program to find the sum of 1+ 1/8 +1/27â€¦â€¦1/n3, where n is the number input by the user.

```Ans.

num = int(input("Enter any number"))
sum = 0
for i in range(1,num + 1):
sum = sum + 1/i**3
print("Sum of series is : ", sum)```

## Q7. Write a program to find the sum of digits of an integer number, input by the user.

```Ans.

num = int(input("Enter any number"))
sum = 0
while(num):
r = num % 10
sum = sum + r
num = num // 10
print("Sum of the digit is : ",sum)```

## Q8. Write a function that checks whether an input number is a palindrome or not.

```Ans.

num = input("Enter any number")
rnum=num[: : -1]
if num == rnum:
print("Palindrome")
else:
print("Not a Palindrome")

OR

num = int(input("Enter any number"))
rnum = 0
r = 0
onum = num
while(num):
r = num % 10
rnum = rnum * 10 + r
num = num // 10
if onum == rnum :
print("Palindrome")
else :
print("Not Palindrome")```

## Q9. Write a program to print the following patterns:

```Ans i)

n = 3
for i in range (1, n + 1):
sp = (n - i) * " "
sym =  (2 * i - 1) * "*"
print(sp, sym)
for j in range(n - 1, 0, -1):
sp = (n - j) * " "
sym = (2 * j - 1)* "*"
print(sp, sym)

ii)

num = 5
for i in range (1, num + 1):
sp = (num - i) * "  "
print(sp, end = " ")
for k in range(i, 1, -1):
print(k, end = " ")
for j in range(1, i + 1):
print(j, end = " ")
print()

iii)

str = "123456"
for i in (str):
print(" " * int(i) + str[0 : len(str) - int(i)])

OR

num = 5
for i in range (num, 0, -1):
sp = (num - i) * "  "
print(sp, end=" ")
for k in range(1, i + 1):
print(k, end=" ")
print()

iv)

num = 3
k = 0
for i in range (1, num + 1):
sp = (num - i)*" "
print(sp, end=' ')
while (k != (2 * i - 1)) :
if (k == 0 or k == 2 * i - 2) :
print("*", end= "")
else :
print(" ", end = "")
k = k + 1
k = 0
print( )
for j in range (num - 1, 0, -1):
sp = (num - j) * " "
print(sp, end=" ")
k = (2 * j - 1)
while (k > 0) :
if (k==1 or k == 2 * j - 1):
print("*",end="")
else:
print(" ",end="")
k = k - 1
print( ) ```

## Q10. 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.

```Ans.

per = int(input("Enter the Percentage"))
if per > 90:
gr="A"
elif per > 80 and per <= 90:
gr = "B"
elif per > 70 and per <= 80:
gr = "C"
elif per >= 60 and per <= 70:
gr = "D"
elif per < 60:
gr = "E"
print("Grade is : ", gr)```

## 3. Find the grade of the student as per the following criteria:

```Ans.

ch='y'
while (ch=='y'):
print("1. Enter Marks of Five Subjects")
print("2. Calculate Percentage")
print("4. Exit")
num = int(input("Enter Your Choice"))
if num == 1 :
en = int(input("Enter marks of English"))
m = int(input("Enter marks of Mathematics"))
sc = int(input("Enter marks of Science"))
h = int(input("Enter marks of Hindi"))
sst = int(input("Enter marks of SST"))
print("Marks in English is :",en)
print("Marks in Hindi is :",h)
print("Marks in Mathematics is :",m)
print("Marks in Science is :",sc)
print("Marks in Social Studies is :",sst)
if num == 2 :
`sum = en + m + sc + h + sst `
`per = sum/5 `
`print("Percentage is ,", per)`
if num == 3 :
if per >= 85 :
elif per >= 75 and per < 85:
elif per >= 50 and per < 75 :
elif per > 30 and per < 50:
elif per < 30:
print("Reappear")
if num == 4:
exit()```

Disclaimer : I tried to give you the correct Answers of “Flow of Control in Python Class 11 NCERT Solution” , but if you feel that there is/are mistakes in the Flow of Control in Python Class 11 NCERT Solution given above, you can directly contact me at csiplearninghub@gmail.com.

Class 12 Computer Science Sample Paper 2020-2021.

Class 12 Computer Science Sample Paper Marking Scheme

Class 12 Computer Science Test Series

Share with others
error: Content is protected !!