Horje
Multiplication Table Using While Loop in Python

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

Output

1 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

Output

1 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.




Reffered: https://www.geeksforgeeks.org


Python

Related
Add Values into an Empty List from Python For Loop Add Values into an Empty List from Python For Loop
Check If API Response is Empty in Python Check If API Response is Empty in Python
Call a Class Method From another Class in Python Call a Class Method From another Class in Python
Print a List in Python Horizontally Print a List in Python Horizontally
Count all Elements in a Nested Python Dictionary Count all Elements in a Nested Python Dictionary

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
11