Python program to generate multiplication Table

Python Program to Generate Multiplication Table

Python Program to Generate a Multiplication Table

Learn how to take user input in Python and use a loop to generate a complete multiplication table dynamically.

Python Source Code

# Get the number from the user
num = int(input("Enter a number to generate its multiplication table: "))

# Get the limit (e.g., up to 10 or 12)
limit = int(input("Enter the limit: "))

print(f"\nMultiplication Table for {num}:")
print("-" * 25)

# Loop to calculate and display each row
for i in range(1, limit + 1):
    print(f"{num} x {i} = {num * i}")

How the Code Works

  • input(): Prompts the user for input and returns it as a string.
  • int(): Converts the input string into an integer for arithmetic calculation.
  • range(1, limit + 1): Creates a sequence starting from 1 up to the specified limit.
  • f"{num} x {i} = {num * i}": An f-string that evaluates the product inline and outputs a formatted result.

Try It Out (Interactive Tester)

Enter your numbers below to test the multiplication table generator directly in your browser:

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

Comments

Popular posts from this blog

Class 6 quiz

Muslim Population Growth in India: A Comprehensive Chronological Analysis (1951–Present)