NCERT Solutions: Flow of Control — CBSE Class 11 Computer Science
This page provides complete NCERT exercise solutions for CBSE Class 11 Computer Science Chapter 6 — Flow of Control. Every question is answered with a clear explanation and working Python code.
How to Use These Solutions
1. Attempt each question yourself first — write the code on paper or in IDLE before looking at the answer. 2. Compare your approach — check the logic, indentation, variable names, and output format. 3. Understand every line — if anything is unclear, revisit the [chapter guide](/class/11/computer-science/cbse-flow-of-control) for the concept. 4. Run the code — type each program in IDLE and verify the output matches what is shown. 5. Fix your mistakes — record errors in a mistake notebook and re-attempt the question the next day.
Key Concepts Quick Review
| Concept | Key Point |
|---|---|
if | Executes block when condition is True |
if-else | Two paths — one for True, one for False |
if-elif-else | Multiple conditions checked top to bottom |
for loop | Use when number of iterations is known |
range(start, stop, step) | Generates integers; stop value is excluded |
while loop | Use when number of iterations is unknown |
break | Exits the innermost loop immediately |
continue | Skips rest of current iteration, goes to next |
pass | Does nothing; placeholder statement |
else with loop | Runs when loop completes without break |
| Nested loops | Inner loop runs fully for each outer iteration |
Exercise Solutions
Q1. Write a program to check whether a number is even or odd.
# Program to check even or odd
num = int(input("Enter a number: "))
if num % 2 == 0:
print(f"{num} is even")
else:
print(f"{num} is odd")
Output:
Q2. Write a program to find the largest of three numbers.
# Program to find the largest of three numbers
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a >= b and a >= c:
largest = a
elif b >= a and b >= c:
largest = b
else:
largest = c
print(f"The largest number is {largest}")
Output:
Q3. Write a program to print the multiplication table of a number entered by the user.
# Program to print multiplication table
num = int(input("Enter a number: "))
print(f"Multiplication table of {num}:")
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
Output:
Q4. Write a program to find the factorial of a number using a loop.
# Program to find factorial using for loop
n = int(input("Enter a number: "))
if n < 0:
print("Factorial is not defined for negative numbers")
else:
fact = 1
for i in range(1, n + 1):
fact *= i
print(f"Factorial of {n} = {fact}")
Output:
Q5. Write a program to print the Fibonacci series up to n terms.
The Fibonacci series starts with 0 and 1. Each next term is the sum of the two previous terms.
# Program to print Fibonacci series
n = int(input("Enter number of terms: "))
if n <= 0:
print("Please enter a positive number")
elif n == 1:
print(0)
else:
a, b = 0, 1
print(a, b, end=" ")
for i in range(2, n):
c = a + b
print(c, end=" ")
a = b
b = c
print()
Output:
Q6. Write a program to print the following star pattern:
# Program to print right-angled triangle star pattern
n = int(input("Enter number of rows: "))
for i in range(1, n + 1):
for j in range(i):
print("*", end=" ")
print()
Output:
Q7. Write a program to check whether a number is prime or not.
A prime number is divisible only by 1 and itself. We check divisibility from 2 to n-1.
# Program to check if a number is prime
n = int(input("Enter a number: "))
if n <= 1:
print(f"{n} is not a prime number")
else:
for i in range(2, n):
if n % i == 0:
print(f"{n} is not a prime number (divisible by {i})")
break
else:
print(f"{n} is a prime number")
Output:
Note: This solution uses the for-else construct. The else block runs only when the loop completes without hitting break, meaning no divisor was found.
Q8. Write a program to find the sum of digits of a number using a while loop.
# Program to find sum of digits
num = int(input("Enter a number: "))
temp = abs(num) # handle negative numbers
digit_sum = 0
while temp > 0:
digit_sum += temp % 10
temp //= 10
print(f"Sum of digits of {num} = {digit_sum}")
Output:
Q9. Write a program to find the reverse of a number using a while loop.
# Program to reverse a number
num = int(input("Enter a number: "))
temp = abs(num)
rev = 0
while temp > 0:
rev = rev * 10 + temp % 10
temp //= 10
if num < 0:
rev = -rev
print(f"Reverse of {num} = {rev}")
Output:
Q10. Write a program to check whether a number is a palindrome.
A palindrome number reads the same forwards and backwards (e.g., 121, 1331).
# Program to check palindrome number
num = int(input("Enter a number: "))
temp = num
rev = 0
while temp > 0:
rev = rev * 10 + temp % 10
temp //= 10
if num == rev:
print(f"{num} is a palindrome")
else:
print(f"{num} is not a palindrome")
Output:
Q11. Write a program to print all prime numbers between two given numbers using nested loops and break.
# Program to print prime numbers in a range
start = int(input("Enter start of range: "))
end = int(input("Enter end of range: "))
print(f"Prime numbers between {start} and {end}:")
for num in range(start, end + 1):
if num <= 1:
continue
for i in range(2, num):
if num % i == 0:
break
else:
print(num, end=" ")
print()
Output:
Q12. Write a program to demonstrate the use of break and continue in a loop.
# Program to demonstrate break and continue
# break: find first number divisible by both 3 and 5
print("Using break:")
for i in range(1, 100):
if i % 3 == 0 and i % 5 == 0:
print(f"First number divisible by both 3 and 5 is {i}")
break
# continue: print numbers 1 to 10, skipping multiples of 3
print("\nUsing continue — numbers 1 to 10 (skip multiples of 3):")
for i in range(1, 11):
if i % 3 == 0:
continue
print(i, end=" ")
print()
Output:
Related Pages
- [Flow of Control — Chapter Guide](/class/11/computer-science/cbse-flow-of-control)
- [Getting Started with Python — Chapter Guide](/class/11/computer-science/cbse-getting-started-python)
- [Getting Started with Python — Exercise Solutions](/class/11/computer-science/solutions/cbse-getting-started-python-exercise-solutions)
