NCERT Solutions for Class 11 Science Computer science Chapter 6 Flow Of Control are provided here with simple step-by-step explanations. These solutions for Flow Of Control are extremely popular among Class 11 Science students for Computer science Flow Of Control Solutions come handy for quickly completing your homework and preparing for exams. All questions and answers from the NCERT Book of Class 11 Science Computer science Chapter 6 are provided here for you for free. You will also love the ad-free experience on Meritnation’s NCERT Solutions. All NCERT Solutions for class Class 11 Science Computer science are prepared by experts and are 100% accurate.

Page No 139:

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.

Page No 139:

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.

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


OUTPUT:
0
1
2
3
​4

Page No 139:

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

Page No 139:

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


​The above statement will create an infinite loop as the value of ‘num’ initially is 20 and each subsequent loop is increasing the value of num by 1, thus making the test condition num > 10 always true.

Page No 139:

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.

OUTPUT:
2
3
4
4
6
8
6
9

vi) OUTPUT: 
Current variable value:  7
Current variable value:  5
Good bye!
Current variable value:  4



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.

Page No 140:

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

 

Page No 140:

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

Page No 140:

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.

Page No 140:

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


 

Page No 140:

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

 



Page No 141:

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

Page No 141:

Question 8:

Write a function that checks whether an input number is a palindrome or not. [Note: A number or a string is called palindrome if it appears same when written in reverse order also. For example, 12321 is a palindrome while 123421 is not a palindrome]

Answer:

Program:
#program to find if the number is a palindrome
rev = 0
n = int(input("Enter the number: "))
#Storing the number input by user in a temp variable to run the loop
temp = n 
#Using while function to reverse the number
while temp > 0:
    digit = (temp % 10)
    rev = (rev * 10) + digit
    temp = temp // 10
#Checking if original number and reversed number are equal
if(n == rev):
    print("The entered number is a palindrome.")
else:
    print("The entered number is not a palindrome.")


OUTPUT:
Enter the Number: 114411
The entered number is a palindrome.

Page No 141:

Question 9:

Write a program to print the following patterns:

 i                  *
             *  *  *  
         *  *  *  *  *
             *  *  *  
                 *
 ii                            1
                        212
                      32123
                    4321234
                  543212345
 iii          1  2  3  4  5
           1  2  3  4
               1  2  3
                   1  2
                       1
 iv                  *
             *      *  
         *             *
             *      *  
                 *

Answer:

i)


# n = 3 Number of lines from top to middle of diamond shape
n = 3
for i in range (1, n + 1):

          # This will print the space  
    space = (n - i)*" "
          # It will print the star
    star =  (2 * i - 1)*"*"
    print(space,star)
# The bottom half will be drawn using this loop
for j in range(n - 1, 0, -1):
    space = (n - j)*" "
    star = (2 * j - 1)*"*"
    print(space, star)


ii) 
n = 5
for i in range (1, n + 1):
    #This will print the space before the number
    space = (n - i) * "  "
    print(space, end = " ")
    #This for loop will print the decreasing numbers
    for in range(i, 1, -1):
        print(k, end = " ")
    
#This for loop will print the increasing numbers
    for j in range(1, i + 1):
        print(j, end = " ")
    #Print a new line after the space and numbers are printed
    print()


iii) 
n = 5
for i in range (n, 0, -1):

        # The space will increase on each iteration as i value will decrease
    space = (n - i)*"  "
    print(space, end=" ")
    #This for loop will print the increasing numbers
    for j in range(1, i + 1):
        print(j, end=" ")
    print()


iv) 
n = 3
k = 0
for i in range (1, n + 1):
    space = (n - i)*" "
    print(space, end=' ')
    while (k != (2 * i - 1)) :
        #This will print the star at the start and end of every line  
        if (k == 0 or k == 2 * i - 2) : 
           
print('*', end= ""
        #This will print the space in the middle
        else
           
print(' ', end = ""
        k = k + 1
    k = 0
   
print()
#This will print the bottom half of the hollow diamond shape

for j in range (n - 1, 0, -1):
    space = (n - j)*" "
   
print(space, end=" ")
    k = (2 * j - 1)
   
while (k > 0) :
        #This will print the star at the start and end of every line 
       
if (k==1 or k == 2*j-1):
           
print("*",end="")
        #This will print the space in the middle
        else:
           
print(" ",end="")
        k = k - 1
   
print
()

Page No 141:

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



View NCERT Solutions for all chapters of Class 11