Examples
- Hello World Program in Python (How to Print Hello World in Python?)
- Python Program to Add Two Numbers (Sum/Addition Program)
- How to Generate Random Number in Python? (Program)
- Leap Year Program in Python (Check Leap Year or Not in Python)
- Python Program to Check Number is Positive, Negative, or Zero
- Even Odd Program in Python (How to Check Number is Even or Odd?)
- Python Program to Find Largest of 3 Numbers (Greatest of Three Numbers in Python)
- Prime Number Program in Python (Check Prime or Not)
- Python Program to Find Square Root of a Number (Examples)
- Python Factorial Program (How to Find Factorial of a Number in Python?)
- Python Program to Find Prime Numbers in Range
- Python Program to Find Area of Triangle (Different Ways With Examples)
- How to Solve Quadratic Equation in Python? (Program Examples)
- How to Swap Two Numbers in Python? (Program Examples to Swap Variables)
- Python Program to Convert Kilometers to Miles (km to mi) and Vice Versa
- Python Program to Convert Celsius To Fahrenheit (and Fahrenheit to Celsius)
- Program to Check Armstrong Numbers in Python (Code Examples)
- Program for Armstrong Number Between Two Intervals in Python
- Program to Print Multiplication Table in Python (Code Examples)
- Python Program for Fibonacci Series (Print Fibonacci Sequence in Python)
- Program for Sum of n Natural Numbers in Python (Code Examples)
- Program to Print Powers of 2 in Python (Code Examples)
- Python Program to Find Numbers Divisible by Another Number
- Convert Decimal to Binary, Octal and Hexadecimal in Python (Program with Example)
- How to Convert Decimal to Binary in Python? Program with Examples
- How to Convert Decimal to Octal in Python? Program with Examples
- How to Convert Decimal to Hexadecimal in Python? Program and Examples
- How to Convert Binary to Decimal in Python? Program With Examples
- How to Convert Hexadecimal to Octal in Python? Program With Examples
- Program to Find LCM of Two Numbers in Python (Calculate LCM in Python)
- GCD of Two Numbers in Python (Program to Find HCF or GCD)
- GCD of Three Numbers in Python (HCF Program for n Numbers)
- Simple Calculator Program in Python (Basic Calculator Code)
- Find Factors of a Number in Python (Programs and Examples)
- Program to Find Prime Factors of a Number in Python
- How to Find ASCII Value in Python? ASCII Value of Character
Program to Print Multiplication Table in Python (Code Examples)
Here is your comprehensive guide on how to print multiplication tables in Python! If you're a beginner or an experienced programmer, understanding the various methods to display multiplication table in Python is an essential skill to have in your coding work.
Multiplication tables are not only fundamental to mathematical calculations but also serve as a powerful learning tool for students. By visualizing and modifying these tables, you can better comprehend the relationships between numbers, spot patterns, and develop a solid foundation in multiplication.
Here, we will explore different ways and Python program to print table of multiplication. The different approaches include use of loops, conditional statements, and even list comprehensions.
Whether you're interested in a concise multiplication table or a more elaborate one with additional formatting, we will provide you with the necessary code examples.
So, without further ado, let's get started and practice the program to print multiplication table in Python.
Print Multiplication Table in Python
Code
# Printing table in Python for given number using for loop and function
def print_multiplication_table(number):
print(f"Multiplication table of {number}:")
for i in range(1, 11):
result = number * i
print(f"{number} x {i} = {result}")
# Example usage
num = int(input("Enter a number: "))
print_multiplication_table(num)
Output
Enter a number: 5
Multiplication table of 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
Explanation
-
In this program, we define a function called print_multiplication_table that takes a parameter number.
-
Inside the function, we use a for loop to iterate from 1 to 10 (inclusive) because we want to print the multiplication table up to 10.
-
For each iteration, we calculate the result by multiplying the number with the loop variable i.
-
Finally, we print the equation number x i = result.
-
To test the program, we prompt the user to enter a number using the input function and convert it to an integer using int(). Then, we call the print_multiplication_table function with the entered number as the argument.
The program will display the multiplication table for the given number.
Multiplication Table in Python Using While Loop
Code
# How to print multiplication table in Python using while loop and function
def multiplication_table(n):
i = 1
while i <= n:
j = 1
while j <= n:
print(i * j, end='\t')
j += 1
print()
i += 1
# Test the program
number = int(input("Enter a number: "))
multiplication_table(number)
Output
Enter a number: 10
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Explanation
-
We define a function multiplication_table(n) that takes a number n as input. We initialize a variable i to 1, which represents the row number, and start a while loop that continues as long as i is less than or equal to n.
-
Within the outer while loop, we initialize another variable j to 1, representing the column number, and start an inner while loop that continues as long as j is less than or equal to n.
-
Inside the inner while loop, we calculate the product of i and j (row number times column number) and print it using the print() function. The end='\t' parameter separates each number with a tab character, creating a tabular structure.
-
After printing all the numbers in a row, we use another print() statement without any arguments to move to the next line and start a new row.
-
The j variable is incremented by 1 in each iteration of the inner while loop, and the i variable is incremented by 1 in each iteration of the outer while loop.
-
To test the program, we prompt the user to enter a number and pass it as an argument to the multiplication_table() function.
When you run this program and input a number, it will generate and print the multiplication table for that number, up to the specified limit, using a while loop.
Multiplication Table in Python Using Lambda Function
Code
print_multiplication_table = lambda number, rows=10: [print(f"{number} x {i} = {number * i}") for i in range(1, rows + 1)]
# Example usage
num = int(input("Enter a number: "))
print_multiplication_table(num)
Output
Enter a number: 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
-
The lambda function print_multiplication_table takes two parameters: number and rows, with rows having a default value of 10.
-
The lambda function uses a list comprehension to iterate over the range of numbers from 1 to rows (inclusive).
-
For each iteration, it prints the equation number x i = number * i.
-
To test the program, we prompt the user to enter a number using the input function and convert it to an integer using int(). Then, we call the print_multiplication_table lambda function with the entered number. Since we don't provide a value for rows, it will default to 10 and print the multiplication table for the given number up to 10 rows.
Practice More Python Programs:
- Hello World Program in Python
- Python Program to Add Two Numbers
- Generate Random Number in Python
- Leap Year Program in Python
- Even Odd Program in Python
- Python Program to Find Largest of 3 Numbers
- Prime Number Program in Python
- Python Program to Find Square Root of a Number
- Python Factorial Program
- Python Program to Find Area of Triangle
- Solve Quadratic Equation in Python
- Swap Two Numbers in Python
- Python Program to Convert Kilometers to Miles
- Python Program to Convert Celsius To Fahrenheit
- Check Armstrong Numbers in Python
- Python Program for Fibonacci Series
- Convert Decimal to Binary, Octal and Hexadecimal in Python
- Find LCM of Two Numbers in Python
- GCD of Two Numbers in Python
- Simple Calculator Program in Python
- Python Matrix Addition Program
- Transpose of Matrix in Python
- Python Matrix Multiplication
- Python Calendar Program