Introduction
A multiplication table is a fundamental concept in mathematics that shows the product of two numbers. This tutorial will guide you through creating a Python program that generates and displays the multiplication table for a given number.
Problem Statement
Create a Python program that:
- Takes a number as input.
- Generates and displays the multiplication table for that number up to a specified range.
Example:
- Input:
number = 5,range = 10 - Output:
5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 ... 5 x 10 = 50
Solution Steps
- Take Input for the Number and Range: Use the
input()function to get the number and the range up to which the multiplication table should be generated. - Convert Input to Integers: Convert the input strings to integers using
int(). - Generate the Multiplication Table: Use a loop to iterate through the range and calculate the product of the number with each value.
- Display the Multiplication Table: Use the
print()function to display the results in a formatted manner.
Python Program
# Python Program to Display the Multiplication Table
# Author: https://www.rameshfadatare.com/
# Step 1: Take input for the number and the range
number = int(input("Enter the number for the multiplication table: "))
table_range = int(input("Enter the range for the multiplication table: "))
# Step 2: Generate and display the multiplication table
print(f"Multiplication Table for {number} up to {table_range}:")
for i in range(1, table_range + 1):
print(f"{number} x {i} = {number * i}")
Explanation
Step 1: Take Input for the Number and Range
- The
input()function prompts the user to enter the number for which they want to generate the multiplication table and the range up to which the table should be displayed. The inputs are converted to integers usingint().
Step 2: Generate and Display the Multiplication Table
- The program uses a
forloop to iterate from 1 to the specified range. For each iteration, it calculates the product of the given number and the current loop index (i). - The
print()function is used to display each line of the multiplication table in the format:number x i = product.
Output Example
Example:
Enter the number for the multiplication table: 5
Enter the range for the multiplication table: 10
Multiplication Table for 5 up to 10:
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
Example:
Enter the number for the multiplication table: 7
Enter the range for the multiplication table: 12
Multiplication Table for 7 up to 12:
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
7 x 11 = 77
7 x 12 = 84
Conclusion
This Python program demonstrates how to generate and display a multiplication table for a given number. It’s a straightforward example that helps beginners understand loops, user input handling, and basic arithmetic operations in Python.