Use C language to print ninety-nine multiplication table

The 9*9 multiplication table can be printed out by using the branch and loop knowledge of C language, the effect is as shown in the figure

Specific code:

Can deeply understand the application of loops and nested loops

int main()
{
	int i = 0;//行数
	for (i = 1; i <= 9; i++)//行数  打印9行
	{
		int j = 0;//列数
		for (j = 1; j <= i; j++)//有几行就打印几列
		{
			printf("%d*%d=%-2d ", i, j, i*j);//%2d 打印两位靠右对齐,%-2d 打印两位靠左对齐
		}
		printf("\n");//每行末尾回车
	}
 
	return 0;
}

i is the number of rows, for (i = 1; i <= 9; i++) This loop statement controls the number of rows, starting from 1 and adding to 9, entering the loop body, setting the number of columns, and analyzing according to the nine-nine multiplication table It can be seen that there are several rows and there are several columns, set the variable j as the number of columns, for (j = 1; j <= i; j++)// print several columns if there are several rows, and j is also added from 1 to 9, Print i*j only when j is less than or equal to i, through printf("%d*%d=%-2d ", i, j, i*j);//%2d print two digits aligned to the right, %- 2d print two digits left-aligned, beautify the output, wrap after printing to achieve a newline on each line.

Among them, some layout beautifications have been made to this nine-nine multiplication table, %-2d prints two digits to the left

Guess you like

Origin blog.csdn.net/qq_44928278/article/details/120091175