Generating a 99 Multiplication Table in C Language

The 99 multiplication table is a classic case for practicing loop structures, easily implemented through nested loops.

Structure Analysis of the 99 Multiplication Table

  • Row 1: 1×1=1
  • Row 2: 1×2=2 2×2=4
  • Row 3: 1×3=3 2×3=6 3×3=9
  • Row 9: 1×9=9 … 9×9=81

Pattern: The i-th row contains i expressions, each expression is<span>j×i=result</span> (where j ranges from 1 to i)

Implementation Approach

  1. Use an outer loop to control the number of rows (i from 1 to 9)
  2. Use an inner loop to control the number of expressions in each row (j from 1 to i)
  3. In the inner loop, output in the format<span>j×i=j*i</span>
  4. After completing the output of each row, move to the next line

Generating a 99 Multiplication Table in C Language

Code Analysis

  1. Outer Loop:<span>for (int i = 1; i <= 9; i++)</span>

  • The variable i represents the multiplier for the current row (from 1 to 9)
  • Controls the loop to execute 9 times, corresponding to 9 rows of content
  • Inner Loop:<span>for (int j = 1; j <= i; j++)</span>

    • The variable j represents the multiplicand (from 1 to i)
    • The i-th row will execute i times, corresponding to i multiplication expressions
  • Output Statement:<span>printf("%d×%d=%d\t", j, i, j * i)</span>

    • <span>%d</span> is used to output integers
    • <span>×</span> is the multiplication symbol
    • <span>\t</span> is a tab character used to align columns for neater output
    • The result of the expression is calculated by<span>j * i</span>
  • Newline Statement:<span>printf("\n")</span>

    • After completing the output of each row, a newline is executed to start the next row

    Variant Implementation: Inverted Triangle Multiplication Table

    To output the inverted triangle form of the 99 multiplication table in reverse order, simply modify the loop conditions:Generating a 99 Multiplication Table in C Language

    Conclusion

    Through the implementation of the 99 multiplication table, we can master:

    1. The usage of nested loops (outer controls rows, inner controls columns)
    2. The application of loop variables within loops
    3. <span>printf</span> function’s formatted output (especially the use of<span>\t</span> tab character)
    4. The application of simple mathematical operations in programs

    This exercise, while simple, excellently demonstrates the essence of loop structures and serves as a great example for beginners to understand nested loops.

    Leave a Comment