Here we use the for loop to print the 9*9 multiplication table
First of all, let's determine the format of the 9*9 multiplication table
We can know that the first row of the multiplication table has 1 column, the second row has 2 columns, the third row has 3 columns...and so on, the ninth row has 9 columns
Can we see the pattern?
We can use the nesting of for loops to achieve this
First make sure to use 2 for loops
two loop variables
One controls the number of rows, and one controls the number of columns in each row
#include<stdio.h>
int main()
{
int i;//行
int j;//列
for (i = 1; i <= 9; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d*%d=%-2d ", i,j,i*j);
}
printf("\n");
}
}
The outer loop controls a total of 9 rows, and the inner loop controls the number of columns on each row
The outer loop loops once, and after the inner loop loops once, print out the first column of the first row.
Then the program continues to go down, and when it encounters printf("\n"); it executes a newline operation, so that after the number of columns on each line has been executed, it immediately executes a newline to print the number of columns on the next line.
Until the outer for loop does not meet the condition, the outer for loop does not meet the condition, and the inner for loop will not execute
This completes the printing of our entire multiplication table.