需求:輸入正整數 n,打印 n×n 乘法表(下三角形式)。
#include <stdio.h>
void print_mul_table(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%d*%d=%-2d ", i, j, i * j); // %-2d 左對齊寬度2
}
printf("\n");
}
}
int main() {
int n;
printf("請輸入乘法表行數:");
scanf("%d", &n);
print_mul_table(n);
return 0;
}
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
%-2d:左對齊,佔 2 字元寬度,保證列對齊。j <= n 並加條件 if (j <= i) print; else print spaces。