Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Seven Approaches to Implement a Multiplication Table in Python

Tech 3

Method 1: Nested For Loops

for row in range(1, 10):
    for col in range(1, row + 1):
        print(f"{row}×{col}={row*col}", end='\t')
    print()

Method 2: Nested While Loops

row = 1
while row <= 9:
    col = 1
    while col <= row:
        print(f"{row}×{col}={row*col}", end='\t')
        col += 1
    print()
    row += 1

Method 3: Mixeed While-To Loop

row = 1
while row <= 9:
    for col in range(1, row + 1):
        print(f"{row}×{col}={row*col}", end=' ')
    print()
    row += 1

Method 4: Mixed For-While Loop

for multiplier in range(1, 10):
    multiplicand = 0
    while multiplicand < multiplier:
        multiplicand += 1
        print(f"{multiplier}×{multiplicand}={multiplier*multiplicand}", end=' ')
    print()

Method 5: Using a List Variable

digits = list(range(1, 10))
for row_val in digits:
    col_val = 1
    while col_val <= row_val:
        print(f"{row_val}×{col_val}={row_val*col_val}", end='\t')
        col_val += 1
    print()

Method 6: Recursive Function

def generate_table(current_row):
    if current_row > 9:
        return
    for col in range(1, current_row + 1):
        print(f"{col}×{current_row}={col*current_row}", end='\t')
    print()
    generate_table(current_row + 1)

generate_table(1)

Method 7: Single-Line Implementation

print('\n'.join([' '.join([f"{col}×{row}={row*col}" for col in range(1, row + 1)]) for row in range(1, 10)]))

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.