This page contains the NCERT Computer Science class 11 chapter 6 Flow of Control. You can find the solutions for the chapter 6 of NCERT class 11 Computer Science Exercise. So is the case if you are looking for NCERT class 11 Computer Science related topic Flow of Control questions and answers for the Exercise
Exercise
1) What is the difference between
else and elif construct of if statement?1)
`else`•
`else` is the default (alternative) block.•
It runs only when the
`if` (and all `elif`, if present) conditions are False.•
No condition is written with
`else`.Syntax:
if condition:
statements
else:
statements
2)
`elif`•
`elif` means “else if”.•
It is used to check one more condition when the previous
`if` condition is False.•
You can write many
`elif` blocks (as needed).•
Each
`elif` has its own condition.Syntax:
if condition1:
statements
elif condition2:
statements
elif condition3:
statements
else:
statements
Key differences (quick comparison)
Basis
`elif``else`Condition needed?
✅ Yes
❌ No
Purpose
Check another condition
Runs when no condition is true
How many times can we use?
Many
`elif`Only one
`else` (optional)Position
Between
`if` and `else`Always at the end
In an
`if–elif–else` chain, only one block runs: the first condition that becomes True; otherwise `else` runs.Example
marks = int(input("Enter marks: "))
if marks >= 90:
print("Grade A")
elif marks >= 80:
print("Grade B")
else:
print("Grade C")
•
If
`marks` is 92 → Grade A•
If
`marks` is 85 → Grade B•
If
`marks` is 70 → Grade C2) What is the purpose of range() function? Give one example.
The purpose of `range()` is to generate a sequence of integers (numbers) which is commonly used in a `for` loop.
•
`range(start, stop, step)`
•
stop is not included
for i in range(1, 6):
print(i)
This prints:
1
2
3
4
5
3) Differentiate between
break and continue statements using examples.In Python, `break` and `continue` are loop control statements used inside `for` and `while` loops to change the normal flow of execution.
1) `break` statement
•
Meaning: Stop the loop immediately.
•
When `break` is executed, the loop terminates at once, and control comes outside the loop.
Example (`break` in a `for` loop):
for i in range(1, 6):
if i == 4:
break
print(i)
print("Loop ended")
Output:
1
2
3
Loop ended
✅ Here, as soon as
`i` becomes 4, the loop stops completely.2) `continue` statement
•
Meaning: Skip the current iteration and move to the next one.
•
When `continue` is executed, Python skips the remaining statements of that iteration and jumps to the next iteration of the loop.
Example (`continue` in a `for` loop):
for i in range(1, 6):
if i == 4:
continue
print(i)
print("Loop ended")
Output:
1
2
3
5
Loop ended
✅ Here, when
`i` becomes 4, only that iteration is skipped, but the loop continues for 5.Key Difference (Quick Comparison)
Basis
`break`continue`What it does
Ends the loop immediately
Skips the current iteration
Control goes to
Outside the loop
Next iteration of the loop
Loop runs further?
❌ No
✅ Yes
(Extra) Same idea using a `while` loop
`break` in `while`:
i = 1
while i <= 5:
if i == 4:
break
print(i)
i += 1
Output:
1
2
3
`continue` in `while`:
i = 0
while i < 5:
i += 1
if i == 4:
continue
print(i)
Output:
1
2
3
5
4) What is an infinite loop? Give one example.
An infinite loop is a loop that never stops because its condition never becomes false.
Example:
while True:
print("This will run forever")
5) Find the output of the following program segments:
(i)
a = 110while 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 = 0while i < 9:if i % 4 == 0:sum = sum + ii = i + 2print (sum)(v)
for x in range(1,4):for y in range(2,5):if x * y > 10:breakprint (x * y)(vi)
var = 7while var > 0:print ('Current variable value: ', var)var = var -1if var == 3:breakelse:if var == 6:var = var -1continueprint ("Good bye!")(i) Code:
a = 110
while a > 100:
print(a)
a -= 2
Output:
110
108
106
104
102
(ii) Code:
for i in range(20, 30, 2):
print(i)
Output:
20
22
24
26
28
(iii) Code:
country = 'INDIA'
for i in country:
print(i)
Output:
I
N
D
I
A
(iv) Code:
i = 0; sum = 0
while i < 9:
if i % 4 == 0:
sum = sum + i
i = i + 2
print(sum)
Working (values of i): 0, 2, 4, 6, 8
Add only when divisible by 4 → 0 + 4 + 8 = 12
Output:
12
(v) Code:
for x in range(1,4):
for y in range(2,5):
if x * y > 10:
break
print(x * y)
Output:
2
3
4
4
6
8
6
9
(vi) Code:
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!")
Output:
Current variable value: 7
Current variable value: 5
Good bye!
Current variable value: 4
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).
# Program to check driving license eligibility
# Eligible age = 18 years
name = input("Enter your name: ")
age = int(input("Enter your age: "))
if age >= 18:
print(name + ", you are eligible to apply for a driving license.")
else:
print(name + ", you are not eligible to apply for a driving license.")
Sample Output (Example)
Case 1 (Eligible):
Enter your name: Rahul
Enter your age: 19
Rahul, you are eligible to apply for a driving license.
Case 2 (Not Eligible):
Enter your name: Asha
Enter your age: 16
Asha, you are not eligible to apply for a driving license.
2) Write a function to print the table of a given number. The number has to be entered by the user.
✅ Program (Python)
# Function to print the multiplication table of a number
def print_table(n):
"""This function prints the table of the given number from 1 to 10."""
for i in range(1, 11):
print(n, "x", i, "=", n * i)
# Taking input from the user
num = int(input("Enter a number: "))
# Calling the function
print_table(num)
Sample Output
Enter a number: 7
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
3) Write a program that prints minimum and maximum of five numbers entered by the user.
✅ Program (Python) — All 5 inputs inside the `for` loop (No lists, no `min()`/`max()`)
# Program to input five numbers and print the minimum and maximum
# All five numbers will be taken inside a for loop.
# We will use two variables: minimum and maximum
# For the first number, we will set both minimum and maximum to that number.
# From the second number onward, we will compare and update.
minimum = None # not set yet
maximum = None # not set yet
# Loop runs 5 times to take 5 numbers as input
for i in range(1, 6):
num = int(input("Enter number " + str(i) + ": "))
# If this is the first number, set both minimum and maximum
if i == 1:
minimum = num
maximum = num
else:
# Compare with current minimum
if num < minimum:
minimum = num
# Compare with current maximum
if num > maximum:
maximum = num
# Print final answers
print("Minimum =", minimum)
print("Maximum =", maximum)
Sample Output
Enter number 1: 25
Enter number 2: 11
Enter number 3: 78
Enter number 4: 4
Enter number 5: 50
Minimum = 4
Maximum = 78
4) Write a program to check if the year entered by the user is a leap year or not.
✅ Program (Python)
# Program to check if a given year is a leap year or not
# A year is a leap year if:
# 1. It is divisible by 400, OR
# 2. It is divisible by 4 AND NOT divisible by 100
year = int(input("Enter a year: "))
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
print(year, "is a leap year")
else:
print(year, "is not a leap year")
Sample Output
Case 1 (Leap Year):
Enter a year: 2020
2020 is a leap year
Case 2 (Not a Leap Year):
Enter a year: 2021
2021 is not 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.(Here, we generate n terms.)
✅ Program (Python)
# Program to generate the sequence: -5, 10, -15, 20, -25, ...
# Pattern: Multiply index by 5, then alternate signs (odd indices are negative, even are positive)
n = int(input("Enter n (number of terms): "))
# Loop from 1 to n (inclusive)
for i in range(1, n + 1):
term = 5 * i # Multiply by 5 to get magnitude: 5, 10, 15, 20, 25, ...
# Check if i is odd (index 1, 3, 5, ... are negative)
if i % 2 == 1:
term = -term # Make it negative for odd positions
print(term, end=" ") # Print with space separator
Sample Output
Enter n (number of terms): 7
-5 10 -15 20 -25 30 -35
6) Write a program to find the sum of 1 + 1/8 + 1/27……1/n³, where n is the number input by the user.
✅ Program (Python)
# Program to find the sum: 1/1³ + 1/2³ + 1/3³ + ... + 1/n³
n = int(input("Enter n: "))
s = 0.0 # Initialize sum to 0.0 (float for decimal calculations)
# Loop from 1 to n (inclusive)
for i in range(1, n + 1):
s += 1 / (i ** 3) # Add 1/i³ to the sum (i**3 means i cubed)
print("Sum =", s)
Sample Output
Enter n: 4
Sum = 1.1776785714285714
7) Write a program to find the sum of digits of an integer number, input by the user.
✅ Program (Python)
# Program to find the sum of digits of an integer number
# Use % 10 to get last digit, // 10 to remove it
num = int(input("Enter an integer: "))
num = abs(num) # Handle negative numbers by taking absolute value
s = 0 # Initialize sum to 0
# Extract digits one by one from right to left
while num > 0:
digit = num % 10 # Get the last digit
s += digit # Add it to sum
num //= 10 # Remove the last digit
# Step 5: Display the result
print("Sum of digits =", s)
Sample Output
Example 1:
Enter an integer: 238
Sum of digits = 13
Example 2:
Enter an integer: 507
Sum of digits = 12
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]
✅ Program (Python)
# Program to check whether a number is a palindrome or not
# Reverse the number using % (modulus) and // (floor division)
# and then compare it with the original number
num = int(input("Enter a number: "))
# Negative numbers are not treated as palindrome
if num < 0:
print(num, "is not a palindrome number.")
else:
original = num # Store the original number safely
reverse = 0 # This will store the reversed number
# Reverse the number digit by digit
while num > 0:
digit = num % 10 # Get the last digit
reverse = reverse * 10 + digit
num = num // 10 # Remove the last digit
# Compare original number with reversed number
if reverse == original:
print(original, "is a palindrome number.")
else:
print(original, "is not a palindrome number.")
Sample Output
Case 1 (Palindrome):
Enter a number: 12321
12321 is a palindrome number.
Case 2 (Not Palindrome):
Enter a number: 456
456 is not a palindrome number.
9) Write a program to print the following patterns:
(i)
*
* * *
* * * * *
* * *
*
* * *
* * * * *
* * *
*
(ii)
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
(iii)
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
1 2 3 4
1 2 3
1 2
1
(iv)
*
* *
* *
* *
*
* *
* *
* *
*
(i) Filled Diamond of `*`
# Pattern (i): Filled diamond using stars
n = 3 # middle row will have (2*n - 1) stars -> 5
# Top half (including middle row)
for i in range(1, n + 1):
# Print spaces before stars
for sp in range(n - i):
print(" ", end="")
# Print stars in odd count: 1, 3, 5 ...
for st in range(2 * i - 1):
print("* ", end="")
print() # move to next line
# Bottom half
for i in range(n - 1, 0, -1):
# Print spaces before stars
for sp in range(n - i):
print(" ", end="")
# Print stars in odd count: 3, 1 ...
for st in range(2 * i - 1):
print("* ", end="")
print()
Output:
*
* * *
* * * * *
* * *
*
(ii) Number Pyramid (Decreasing then Increasing)
# Pattern (ii): Number pyramid
# Each row prints: spaces, decreasing numbers (i to 1), increasing numbers (2 to i)
n = 5
for i in range(1, n + 1):
# Print spaces (each space block is " " for alignment)
for sp in range(n - i):
print(" ", end="")
# Print decreasing numbers: i, i-1, ..., 2, 1
for k in range(i, 0, -1):
print(k, end=" ")
# Print increasing numbers: 2, 3, ..., i
for j in range(2, i + 1):
print(j, end=" ")
print() # next line
Output:
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
(iii) Decreasing Rows of `1 2 3 …`
# Pattern (iii): Print numbers from 1 to i, but rows decrease from 5 to 1
n = 5
for i in range(n, 0, -1): # i = 5, 4, 3, 2, 1
for j in range(1, i + 1): # print 1 to i
print(j, end=" ")
print() # next line
Output:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
(iv) Hollow Diamond of `*`
# Pattern (iv): Hollow diamond outline using stars
n = 3 # half height
# Top part (rows 1 to n-1)
for i in range(1, n):
# Spaces before first star
for sp in range(n - i):
print(" ", end="")
print("*", end="") # first star
# Spaces between two stars (only from row 2 onward)
if i > 1:
for mid in range(2 * i - 3):
print(" ", end="")
print("*", end="") # second star
print() # next line
# Middle row (always two stars with gap)
print("*", end="")
for mid in range(2 * n - 3):
print(" ", end="")
print("*")
# Bottom part (rows n-1 down to 1)
for i in range(n - 1, 0, -1):
# Spaces before first star
for sp in range(n - i):
print(" ", end="")
print("*", end="") # first star
if i > 1:
for mid in range(2 * i - 3):
print(" ", end="")
print("*", end="") # second star
print()
Output:
*
* *
* *
* *
*
9. 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.
# Read percentage of marks from the user
p = float(input("Enter percentage: "))
# Decide grade based on the given ranges
if p > 90:
print("Grade A")
elif p >= 80:
print("Grade B")
elif p >= 70:
print("Grade C")
elif p >= 60:
print("Grade D")
else:
print("Grade E")
Sample Run Output (Example 1)
Enter percentage: 95
Grade A
Sample Run Output (Example 2)
Enter percentage: 85
Grade B
Sample Run Output (Example 3)
Enter percentage: 55
Grade E
Case Study-based Question
Let us add more functionality to our SMIS developed in Chapter 5.
6.1 Write a menu driven program that has options to
•
accept the marks of the student in five major subjects in Class X and display the same.
•
calculate the sum of the marks of all subjects. Divide the total marks by number of subjects (i.e. 5), calculate percentage = total marks/5 and display the percentage.
•
Find the grade of the student as per the following criteria:
Criteria
Grade
percentage > 85
A
percentage < 85 && percentage >= 75
B
percentage < 75 && percentage >= 50
C
percentage > 30 && percentage <= 50
D
percentage < 30
Reappear
Let’s peer review the case studies of others based on the parameters given under “DOCUMENTATION TIPS” at the end of Chapter 5 and provide a feedback to them.
# SMIS menu-driven program: accept marks, compute total/percentage and grade
marks = []
total = 0
percentage = 0
while True:
# display menu options to the user
print("\n--- SMIS MENU ---")
print("1. Accept marks (5 subjects) and display")
print("2. Calculate total and percentage")
print("3. Display grade")
print("4. Exit")
choice = int(input("Enter your choice (1-4): "))
# Option 1: accept marks for 5 subjects
if choice == 1:
marks = []
# read marks into the list
for i in range(5):
m = float(input(f"Enter marks for subject {i+1}: "))
marks.append(m)
print("Marks entered:", marks)
# Option 2: calculate total and percentage
elif choice == 2:
if len(marks) != 5:
print("Please enter marks first using option 1.")
else:
total = sum(marks)
percentage = total / 5
print("Total marks =", total)
print("Percentage =", percentage)
# Option 3: compute and display grade based on percentage
elif choice == 3:
if len(marks) != 5:
print("Please enter marks first using option 1.")
else:
# if percentage not calculated yet, calculate it
total = sum(marks)
percentage = total / 5
# determine grade using the given ranges
if percentage > 85:
grade = "A"
elif percentage < 85 and percentage >= 75:
grade = "B"
elif percentage < 75 and percentage >= 50:
grade = "C"
elif percentage > 30 and percentage <= 50:
grade = "D"
else:
grade = "Reappear"
print("Percentage =", percentage)
print("Grade =", grade)
# Option 4: exit the program
elif choice == 4:
print("Exiting... Bye Bye!!")
break
# invalid choice handling
else:
print("Wrong choice. Please enter between 1 and 4.")
Sample Output
--- SMIS MENU ---
1. Accept marks (5 subjects) and display
2. Calculate total and percentage
3. Display grade
4. Exit
Enter your choice (1-4): 1
Enter marks for subject 1: 82
Enter marks for subject 2: 90
Enter marks for subject 3: 75
Enter marks for subject 4: 88
Enter marks for subject 5: 80
Marks entered: [82.0, 90.0, 75.0, 88.0, 80.0]
--- SMIS MENU ---
1. Accept marks (5 subjects) and display
2. Calculate total and percentage
3. Display grade
4. Exit
Enter your choice (1-4): 2
Total marks = 415.0
Percentage = 83.0
--- SMIS MENU ---
1. Accept marks (5 subjects) and display
2. Calculate total and percentage
3. Display grade
4. Exit
Enter your choice (1-4): 3
Percentage = 83.0
Grade = B
--- SMIS MENU ---
1. Accept marks (5 subjects) and display
2. Calculate total and percentage
3. Display grade
4. Exit
Enter your choice (1-4): 4
Exiting... Bye Bye!!