Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
+2 votes
3.1k views
in Computer by (43.0k points)
closed by

NCERT Solutions Class 11, Computer Science, Chapter- 6, Flow of Control

To thoroughly grasp this chapter and excel in CBSE exams and competitive tests, utilizing NCERT Solutions is highly recommended. These solutions, crafted by experts in the field, delve into all key concepts covered in the chapter. Specifically designed for the CBSE curriculum, they ensure a comprehensive understanding and invaluable support in your academic endeavors.

In these NCERT Solutions for Class 11 Computer Science, we have discussed all types of NCERT intext questions and exercise questions.

Concepts covered in Class 11 Computer Science chapter- 6 Flow of Control, are :

  • Introduction to Flow of Control
  • Selection
  • Indentation
  • Repetition
  • Break and Continue Statements
  • Nested Loops

Our NCERT Solutions for Class 11 Computer Science provide detailed explanations to assist students with their homework and assignments. Proper command and ample practice of topic-related questions provided by our NCERT solutions is the most effective way to achieve full marks in your exams. Begin studying right away to ace your exams.

Easily access all solutions and practice questions at your fingertips to kick-start your preparation immediately.

3 Answers

+2 votes
by (43.0k points)
selected by
 
Best answer

NCERT Solutions Class 11, Computer Science, Chapter- 6, Flow of Control

Exercise

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

Solution:

‘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 the if..else statement.

If more than one statements need to be evaluated, the ‘elif’ construct is used. There is no limit on the number of ‘elif’ constructs that 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.

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

Solution:

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 the stop value (excluding the 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

3. Differentiate between break and continue statements using examples.

Solution:

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

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

Solution:

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.

5. Find the output of the following program segments:

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

Solution:

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) for i in range(20,30,2):
 print(i)

Solution:

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

OUTPUT:
20
22
24
26
28

(iii) country = 'INDIA'
for i in country:
 print (i)

Solution:

The variable 'country' will act as a list of characters for the loop to iterate.

OUTPUT:
I
N
D
I
A

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

Solution:

The ‘while’ loop will run till the value of 'i' is 8, and the condition in the ‘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) for x in range(1,4):
 for y in range(2,5):
 if x * y > 10:
 break
 print (x * y)

Solution:

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

Solution:

OUTPUT: 

Current variable value: 7
Current variable value: 5
Good bye!
Current variable value: 4

+2 votes
by (43.0k points)

Programming Exercises

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

Solution:

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

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

Solution:

#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

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

Solution:

#Program to input five numbers and print the 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 the ‘smallest’ variable, and if greater than the previous value it will be assigned to the 'largest' variable.

After the end of the loop, the values of the ‘smallest’ and ‘largest’ variables 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

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

Solution:

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

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.

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

Solution:

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 the event 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'.

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

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.

Solution:

sum = 0
#Getting the number input from the 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

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

Solution:

The program can be written in two ways.

The number entered by the user can be converted to an integer and then by using the 'modulus' and 'floor' operators it can be added digit by digit to a variable 'sum'.
The number is iterated as a string and just before adding it to the '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 the 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.
+2 votes
by (43.0k points)

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]

Solution:

#program to find if the number is a palindrome
rev = 0
n = int(input("Enter the number: "))
#Storing the number input by the user in a temp variable to run the loop
temp = n 
#Using the while function to reverse the number
while temp > 0:
    digit = (temp % 10)
    rev = (rev * 10) + digit
    temp = temp // 10
#Checking if the 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.

9. Write a program to print the following patterns:

(i)

 pyramid pattern

Solution:

pattern

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

triangle pattern

Solution:

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 k 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)

pattern

Solution:

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)

pattern

Solution:

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()

10. Write a program to find the grade of a student when grades are allocated as given in the table below.

Percentage of Marks Grade
Above 90% A
80% to 90% B
70% to 80% C
60% to 70% D
Below 60% E

Percentage of the marks obtained by the student is input to the program.

Solution:

#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

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...