Multiplication tables are fundamental in mathematics and are often required in various applications. In this article, we'll explore how to write a Python program to display the multiplication table for a given number.
Steps to Print Multiplication Table in Python.
We can print a multiplication table of any number (from 1 to 10) in Python using a loop and in our example, we are going to use for loop.
Below are the steps to follow:
- Accept a number from the user for which you want to print the table.
- Use a loop to iterate from 1 to the desired range (example: 1 to 10).
- Multiply the input number by the current iteration variable in each step.
- Display the result in the desired format of a multiplication table.
Python Code:
# Python Program to Display the Multiplication Table # Step 1: Accept a number from the user number = int(input("Enter the number: ")) # Step 2: Display the multiplication table up to 10 print(f"Multiplication Table for {number}:") for i in range(1, 11): result = number * i print(f"{number} x {i} = {result}")
Enter the number: 12 Multiplication Table for 12: 12 x 1 = 12 12 x 2 = 24 12 x 3 = 36 12 x 4 = 48 12 x 5 = 60 12 x 6 = 72 12 x 7 = 84 12 x 8 = 96 12 x 9 = 108 12 x 10 = 120
Explanation:
In the above example, we use the input() function to get a number from the user. The for loop runs from 1 to 10 (inclusive) to generate the multiplication table. Inside the loop, we multiply the user-entered number by the current loop variable (i) to calculate the result. The print() statement displays the multiplication table in the required format.
Trends is an amazing magazine Blogger theme that is easy to customize and change to fit your needs.
No comments
Post a Comment