Multiplication Table Using While Loop in Python
Last Updated :
07 Mar, 2024
Multiplication tables are fundamental in mathematics, serving as the building blocks for more advanced concepts. In Python, we can use various methods to generate multiplication tables, and one versatile tool for this task is the 'while' loop. In this article, we will explore some commonly used and straightforward methods to create multiplication tables using while loops.
Multiplication Table Using While Loop In Python
Below, are the methods of Multiplication Table Using While Loop In Python.
Multiplication Table Using Basic While Loop
In this example basic while loop generates and prints the multiplication table for the 5 times table, iterating from 1 to 10, demonstrating a fundamental use of while loops for repetitive tasks in Python.
Python3
# Basic While Loop
multiplier = 5
counter = 1
while counter <= 10:
result = counter * multiplier
print(f"{counter} x {multiplier} = {result}")
counter += 1
Output1 x 5 = 5
2 x 5 = 10
3 x 5 = 15
4 x 5 = 20
5 x 5 = 25
6 x 5 = 30
7 x 5 = 35
8 x 5 = 40
9 x 5 = 45
10 x 5 = 50
Multiplication Table Using User-Defined While Loop
In this example, user-defined while loop takes user input for a multiplier and a specified range, then generates and prints the corresponding multiplication table, showcasing the flexibility of while loops in customizing repetitive tasks based on user preferences in Python.
Python3
# User-Defined While Loop
multiplier = int(input("Enter the multiplier: "))
start = 1
end = int(input("Enter the range end: "))
while start <= end:
result = start * multiplier
print(f"{start} x {multiplier} = {result}")
start += 1
Output
Enter the multiplier: 5
Enter the range end: 5
1 x 5 = 5
2 x 5 = 10
3 x 5 = 15
4 x 5 = 20
5 x 5 = 25
Multiplication Table Using Nested While Loop
In this example, This nested while loop produces a left-to-right formatted multiplication table, iterating through rows and columns from 1 to 10, showcasing the versatility of while loops in organizing and presenting tabular data efficiently in Python.
Python3
# Nested While Loop (Left-to-Right Format)
row = 1
while row <= 10:
col = 1
while col <= 10:
result = row * col
print(f"{row} x {col} = {result}", end="\t")
col += 1
print() # Move to the next line for the next row
row += 1
Output1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 1 x 4 = 4 1 x 5 = 5 1 x 6 = 6 1 x 7 = 7 1 x 8 = 8 1 x 9 = 9 1 x 10 = 10
2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 ...
Conclusion
In this article, we explored three different methods for creating multiplication tables using while loops in Python. The basic while loop, user-defined while loop, and nested while loop offer flexibility in generating tables for specific use cases. Understanding these methods provides a solid foundation for tackling more advanced programming challenges involving loops and mathematical operations.
Similar Reads
Reverse Multiplication Table Using For loop in Python A multiplication table of any number can be printed using the For loop in Python. We can also print the multiplication table in reverse order using a for loop in Python. In this article, we will see how we can print the multiplication table in reverse order using a for loop in Python. Example: Input
3 min read
Loop Through a List using While Loop in Python In Python, the while loop is a versatile construct that allows you to repeatedly execute a block of code as long as a specified condition is true. When it comes to looping through a list, the while loop can be a handy alternative to the more commonly used for loop. In this article, we'll explore fou
3 min read
Python Program to Find the Sum of Natural Numbers Using While Loop Calculating the Sum of N numbers in Python using while loops is very easy. In this article, we will understand how we can calculate the sum of N numbers in Python using while loop.Calculate Sum of Natural Numbers in Python Using While LoopBelow are some of the examples by which we can see how we can
2 min read
How to Parallelize a While loop in Python? Parallelizing a while loop in Python involves distributing the iterations of a loop across multiple processing units such as the CPU cores or computing nodes to execute them concurrently. This can significantly reduce the overall execution time of the loop, especially for tasks that are CPU-bound or
2 min read
How to Emulate a Do-while loop in Python? We have given a list of strings and we need to emulate the list of strings using the Do-while loop and print the result. In this article, we will take a list of strings and then emulate it using a Do-while loop. Do while loop is a type of control looping statement that can run any statement until th
3 min read
Python - Constant Multiplication over List We are given a list we need to multiply each element in the list by a constant. For example, we are having a list a = [1, 2, 3, 4, 5] and constant c=2 we need to multiply this constant in whole list.Using List ComprehensionList comprehension allows us to multiply each element in list by a constant b
3 min read