本文共 1416 字,大约阅读时间需要 4 分钟。
1.实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定,输入9,输出9*9口诀表,输出12,输出12*12的乘法口诀表。
#include运行结果如下:#include #pragma warning(disable:4996)int showMaltable(int n){ int i, j; for (i = 1; i <= n; i++) { printf(" "); for (j = 1; j <= i; j++) { printf("%d*%d=%d", i, j, i*j); printf(" "); } printf("\n"); } return i*j;}int main(){ int n; scanf("%d", &n); showMaltable(n); system("pause"); return 0;}
2.使用函数实现两个数的交换。
#include#include #pragma warning(disable:4996)int get_value(int *x, int *y){ *x = *x^*y; *y = *x^*y; *x = *x^*y; return *x, *y;}int main(){ int x; int y; scanf("%d%d", &x, &y); get_value(&x,&y); printf("%d %d\n", x, y); system("pause"); return 0;}
3.实现一个函数判断year是不是润年。
#include#include #pragma warning(disable:4996) #include int isleapYear(int year){ if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { return 1; } return 0;}int main(){ int year=1000; for (year = 1000; year <= 2000; year++) { if (isleapYear(year)) { printf("%d ", year); } } system("pause"); return 0;}
4.写一个函数判断是不是素数。
#include#include #pragma warning(disable:4996) #include int primenumber(int i){ int j = 2; for (j = 2; j <= sqrt(i); j++) { if (i%j == 0) { return 0; break; } else{ return 1 ; } }}int main(){ int i; scanf("%d", &i); i=primenumber(i); printf("%d", i); system("pause"); return 0;}