Class | Subject | Book | Chapter | Session | Questions covered |
|---|---|---|---|---|---|
XI | Computer Science | Computer Science (NCERT) | 6 – Flow of Control | 2026-27 | 10 of 10 |
Chapter at a glance
Idea | Key point |
|---|---|
Flow of control | Order in which statements execute: sequence, selection, repetition |
Selection | if, if…else, if…elif…else; the block is marked by indentation |
Repetition | for loop (fixed number of times, often with range()), while loop (until a condition becomes False) |
range() | range(start, stop, step) — stop is excluded |
break / continue | break exits the loop; continue skips to the next iteration |
Nested loops | A loop inside another loop — used for patterns |
Indentation | Python uses indentation (usually 4 spaces) to group statements |
Exercise Questions
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
Answer
name = input("Enter your name: ")
age = int(input("Enter your age: "))
if age >= 18:
print(name + ", you are eligible to apply for a driving licence.")
else:
print(name + ", you are not eligible. Wait", 18 - age, "more year(s).")
Sample run: Aman, 16 → Aman, you are not eligible. Wait 2 more year(s).
Use if age >= 18 to print “eligible”, else “not eligible”.
Write a function to print the table of a given number. The number has to be entered by the user.
Answer
Answer
def table(num):
for i in range(1, 11):
print(num, "x", i, "=", num * i)
n = int(input("Enter a number: "))
table(n)
Sample run: 7 → 7 x 1 = 7 … 7 x 10 = 70
Define table(num) with a for loop over range(1, 11) printing num × i; call it with the user’s number.
Write a program that prints minimum and maximum of five numbers entered by the user.
Answer
Answer
num = float(input("Enter number 1: "))
smallest = largest = num
for i in range(2, 6):
num = float(input("Enter number " + str(i) + ": "))
if num < smallest:
smallest = num
if num > largest:
largest = num
print("Minimum =", smallest)
print("Maximum =", largest)
Sample run: 12, 5, 30, 8, 19 → Minimum = 5.0, Maximum = 30.0
Take the first number as both min and max, then compare each of the next four numbers and update; print min and max.
Write a program to check if the year entered by the user is a leap year or not.
Answer
Answer
A year is a leap year if it is divisible by 4 but not by 100, or if it is divisible by 400.
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(year, "is a leap year")
else:
print(year, "is not a leap year")
Check: 2024 → leap; 1900 → not leap; 2000 → leap; 2026 → not leap.
Leap year if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0.
Write a program to generate the sequence: –5, 10, –15, 20, –25….. upto n, where n is an integer input by the user.
Answer
Answer
The k-th term is 5k, with a minus sign when k is odd. “Up to n” is taken as n terms.
n = int(input("Enter number of terms: "))
for k in range(1, n + 1):
term = 5 * k
if k % 2 == 1:
term = -term
print(term, end=" ")
Sample run: n = 6 → -5 10 -15 20 -25 30
Term k = 5k, negative when k is odd; loop k from 1 to n (n = 6 → −5 10 −15 20 −25 30).
Write a program to find the sum of 1 + 1/8 + 1/27 …… 1/n³, where n is the number input by the user.
Answer
Answer
n = int(input("Enter n: "))
total = 0
for i in range(1, n + 1):
total += 1 / i ** 3
print("Sum =", total)
Sample run: n = 3 → 1 + 0.125 + 0.037037 = 1.162037…
Add 1/i³ for i = 1 to n in a loop (n = 3 → ≈ 1.162).
Write a program to find the sum of digits of an integer number, input by the user.
Answer
Answer
num = int(input("Enter an integer: "))
num = abs(num) # handle negative numbers
total = 0
while num > 0:
total += num % 10 # last digit
num //= 10 # remove last digit
print("Sum of digits =", total)
Sample run: 4728 → 4 + 7 + 2 + 8 = 21
Repeatedly add num % 10 and do num //= 10 until num is 0 (4728 → 21).
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
Answer
def is_palindrome(num):
original = num
rev = 0
while num > 0:
rev = rev * 10 + num % 10
num //= 10
return rev == original
n = int(input("Enter a number: "))
if is_palindrome(n):
print(n, "is a palindrome")
else:
print(n, "is not a palindrome")
Check: 12321 → reversed 12321 → palindrome; 123421 → reversed 124321 → not a palindrome.
Reverse the number with a while loop (rev = rev × 10 + num % 10) and compare with the original.
Write a program to print the following patterns:
i) a diamond of stars (1, 3, 5, 3, 1 stars)
ii) a number pyramid (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) an inverted number triangle (1 2 3 4 5 / 1 2 3 4 / 1 2 3 / 1 2 / 1)
iv) a hollow diamond of stars
Answer
Answer
i) Diamond
n = 3
for i in list(range(1, n + 1)) + list(range(n - 1, 0, -1)):
print(" " * (n - i) + "* " * (2 * i - 1))
*
* * *
* * * * *
* * *
*
ii) Number pyramid
n = 5
for i in range(1, n + 1):
print(" " * (n - i), end="")
for k in range(i, 0, -1): # descending part
print(k, end=" ")
for k in range(2, i + 1): # ascending part
print(k, end=" ")
print()
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) Inverted triangle
for i in range(5, 0, -1):
print(" " * (5 - i), end="")
for k in range(1, i + 1):
print(k, end=" ")
print()
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
iv) Hollow diamond
n = 3
for i in list(range(1, n + 1)) + list(range(n - 1, 0, -1)):
if i == 1:
print(" " * (n - 1) + "*")
else:
print(" " * (n - i) + "*" + " " * (4 * i - 5) + "*")
*
* *
* *
* *
*
Each pattern uses nested loops: the outer loop controls rows (rising then falling for diamonds), and the inner loops print leading spaces and then stars or numbers (programs and outputs shown above).
Write a program to find the grade of a student when grades are allocated as given in the table below.
Answer
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.
Answer
The table’s boundaries overlap (90 appears in two rows); here 90 counts as B, 80 as C and 70 as D, i.e. “Above 90” is strictly above.
per = float(input("Enter percentage: "))
if per > 90:
grade = 'A'
elif per > 80:
grade = 'B'
elif per > 70:
grade = 'C'
elif per >= 60:
grade = 'D'
else:
grade = 'E'
print("Grade:", grade)
Sample run: 84.5 → Grade: B
Use an if–elif–else ladder: > 90 → A, > 80 → B, > 70 → C, ≥ 60 → D, else E.
Case study 6.1 (SMIS): menu-driven marks program
marks = []
while True:
print("\n1. Enter marks 2. Show percentage 3. Show grade 4. Exit")
ch = int(input("Choice: "))
if ch == 1:
marks = [float(input("Marks in subject " + str(i) + ": ")) for i in range(1, 6)]
print("Marks:", marks)
elif ch == 2 or ch == 3:
if not marks:
print("Enter marks first"); continue
per = sum(marks) / 5
if ch == 2:
print("Total =", sum(marks), "Percentage =", per)
else:
if per > 85: g = 'A'
elif per >= 75: g = 'B'
elif per >= 50: g = 'C'
elif per >= 30: g = 'D'
else: g = 'Reappear'
print("Grade:", g)
elif ch == 4:
break
else:
print("Invalid choice")
(The book’s criteria leave 85 and 30 unassigned; here 85 is treated as B and 30 as D.)
Topic checklist
- ☒ Selection with if–elif–else (Q1, 4, 10)
- ☒ for and while loops (Q2, 3, 5–8)
- ☒ Nested loops and patterns (Q9)
- ☒ Menu-driven programs (case study)