Chapter #6 – Flow of Control

Class 11 – Chapter 6 Solutions – PROGRAMMING EXERCISES #2
Write a function to print the table of a given number. The number has to be entered by the user.
Python Program:
def print_multiplication_table(number):
"""Prints the multiplication table for the given number."""
print(f"Multiplication Table for {number}:")
for i in range(1, 11): # Multiplying from 1 to 10
print(f"{number} x {i} = {number * i}")
# Get user input
try:
num = int(input("Enter a number to print its multiplication table: "))
print_multiplication_table(num)
except ValueError:
print("Please enter a valid integer.")
You can checkout this code from Github here – Programming Exercises – 2
Explanation of the Code
1. Function Definition:
• print_multiplication_table(number): A function that takes one argument,
number, and prints its multiplication table from 1 to 10.
2. For Loop:
• The loop iterates from 1 to 10, multiplying the given number by each value
of i and printing the result in a formatted string.
3. User Input:
• The program prompts the user to enter a number using input().
• It converts the input to an integer. If the input is not a valid integer, it catches the ValueError and prints an error message.
Example Output:
Enter a number to print its multiplication table: 5
Multiplication Table for 5:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
NCERT Computer Science Class 11 – Chapter 6 Solutions – Q 7