1.实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定, 输入9,输出99口诀表,输入12,输出1212的乘法口诀表。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int Mul(int x) {
int sum = 0;
int i;
int j;
for (i = 1; i <= x; ++i) {
for (j = 1; j <= i; ++j) {
printf(" %d*%d=%d ", i, j, ij);
}
printf("\n");
}
}
void main() {
int x;
scanf("%d", &x);
Mul(x);
system(“pause”);
return 0;
}
2.使用函数实现两个数的交换。
#include <stdio.h>
#include <stdlib.h>
int Swap(int x, int* y) {
int tmp = *x;
*x = *y;
*y = tmp;
return 0;
}
void main() {
int a = 15;
int b = 20;
Swap(&a, &b);
printf(“a=%d b=%d\n”, a, b);
system(“pause”);
}
3.实现一个函数判断year是不是润年。
#include <stdio.h>
#include <stdlib.h>
int Is_Leapyear(int year) {
if (year % 100 == 0) {
if (year % 400 == 0) {
return 1;
}
else {
return 0;
}
}
else {
if (year % 4 == 0) {
return 1;
}
else {
return 0;
}
}
}
void main() {
int year;
int tmp= Is_Leapyear(2007);
if (tmp == 1)
printf(“是闰年!\n”);
else
printf(“不是闰年!\n”);
system(“pause”);
}
4.实现一个函数,判断一个数是不是素数。
#include <stdio.h>
#include <stdlib.h>
int Is_Prime(int x) {
if (x == 1 || x <= 0) {
return 0;
}
for (int i = 2; i < (x / 2); ++i) {
if (x%i == 0) {
return 0;
}
}
return 1;
}
void main() {
int tmp = Is_Prime(19);
if (tmp == 1)
printf(“是素数!\n”);
else
printf(“不是素数!\n”);
system(“pause”);
return 0;
}