41 函数封装练习
1,打印金字塔封装
1 #include<stdio.h> 2 void print_tower(int n) { 3 for (int i = 1;i <= n;i++) { 4 for (int k = 1;k <= (n - i);k++) { 5 printf(" "); 6 } 7 for (int j = 1;j <= (2 * i - 1);j++) { 8 if (j == 1 || j == 2 * i - 1 || i == n) { 9 printf("*"); 10 } 11 else { 12 printf(" "); 13 } 14 } 15 printf("\n"); 16 } 17 } 18 void main() { 19 int num = 0; 20 printf("请输入层数:"); 21 scanf("%d", &num); 22 print_tower(num); 23 24 }
2,打印九九乘法表封装
1 #include<stido.h> 2 void print99(int n) { 3 for (int i = 1;i <= n;i++) { 4 for (int j = 1;j <= i;j++) { 5 printf("%d*%d=%d ", j, i, i * j); 6 } 7 printf("\n"); 8 } 9 } 10 void main() { 11 int num = 0; 12 printf("请输入一个数字:"); 13 scanf("%d", &num); 14 print99(num); 15 }