Fundamentals 10 min read

How to Print the 9×9 Multiplication Table with Nested Loops in Python

This article explains step‑by‑step how to use both while‑loops and for‑loops in Python to generate a formatted 9×9 multiplication table, detailing the loop logic, code syntax, and the exact output produced for each row.

Lisa Notes
Lisa Notes
Lisa Notes
How to Print the 9×9 Multiplication Table with Nested Loops in Python

The article demonstrates how to generate the classic 9×9 multiplication table in Python by using nested loops, first with while statements and then with for loops.

While‑loop implementation

Two variables, i (outer loop) and j (inner loop), are initialized to 0. The outer loop runs while i < 9, incrementing i at the start of each iteration so that i takes values 1‑9. Inside, the inner loop runs while j < 9, incrementing j from 1‑9. Each iteration prints the expression i * j = (j*j) followed by a space, and after the inner loop finishes a print() adds a newline.

i=0  # outer loop
while i < 9:
   i += 1
   j = 0  # inner loop
   while j < 9:
      j += 1
      print(i, "*", j, "=", (j*j), end=" ")
   print()

The logic yields nine rows; each row contains nine expressions where the right‑hand side is the square of j. Because the right side does not involve i, the table actually shows the squares of numbers 1‑9 repeated for each i value.

Sample output (first two rows):

1 * 1 = 1 1 * 2 = 4 1 * 3 = 9 1 * 4 = 16 1 * 5 = 25 1 * 6 = 36 1 * 7 = 49 1 * 8 = 64 1 * 9 = 81 
2 * 1 = 1 2 * 2 = 4 2 * 3 = 9 2 * 4 = 16 2 * 5 = 25 2 * 6 = 36 2 * 7 = 49 2 * 8 = 64 2 * 9 = 81 
...

Modified while‑loop version

To print only i entries per row, the inner loop condition is changed to while j < i. The rest of the code remains the same.

i=0  # outer loop
while i < 9:
   i += 1
   j = 0  # inner loop
   while j < i:  # number of items in the current row
      j += 1
      print(i, "*", j, "=", (j*j), end=" ")
   print()

For‑loop implementation

The same table can be produced more concisely with for loops using range(1,10). The outer loop iterates i from 1 to 9, and the inner loop iterates j from 1 to 9, printing i * j = (i*j) and adding a newline after each outer iteration.

for i in range(1, 10):
   for j in range(1, 10):
      print(j, "*", i, "=", (i*j), end=" ")
   print()

The resulting output matches the conventional multiplication table, e.g.:

1 * 1 = 1 
2 * 1 = 2 2 * 2 = 4 
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 
... 
9 * 1 = 9 ... 9 * 9 = 81

Overall, the article walks through the problem setup, explains the role of each loop variable, shows concrete code snippets, and presents the exact printed results for both while and for approaches.

Pythonloopsfor loopwhile-loopnested loopsmultiplication table
Lisa Notes
Written by

Lisa Notes

Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.