函数实现乘法口诀表
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
int ride(int i, int j) {
int result;
result = i * j;
return result;
}
int main() {
int n;
printf("请输入乘法口诀阶数:");
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
printf("%d*%d = %d ", i, j, ride(i,j) );
}
putchar('\n');
}
system("pause");
}
两个数交换
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
void exchange(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
int main() {
int x;
int y;
printf("请输入两个不同的数:\n");
scanf("%d %d", &x,&y);
exchange(&x, &y);
printf("A=%d B=%d\n", x, y);
system("pause");
}
判断闰年
#include
#include
int is_Leapyear(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return 1;
} else {
return 0;
}
}
int main() {
int year;
int a;
printf("请输入想要判断的年份:");
scanf("%d", &year);
if (is_Leapyear(year)) {
printf("闰年\n");
} else {
printf("非闰年\n");
}
system("pause");
}
全局变量令函数返回值为字符转
#include
#include
char gs[100];
char is_Leapyear(int *year) {
if (*year % 4 == 0 && *year % 100 != 0 || *year % 400 == 0) {
strcpy(gs, "是闰年");
} else {
strcpy(gs, "不是闰年");
}
return gs;
}
int main() {
int year;
printf("请输入想要判断的年份:");
scanf("%d", &year);
is_Leapyear(&year);
printf("%s\n",gs );
system("pause");
}
实现函数初始化数组 清空数组 数组元素的逆置
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
void init(int* a) {
for (int n = 0; n < 10; ++n) {
int ran = rand() % 100;
a[n] = ran;
}
}
void empty(int* a) {
for (unsigned int n = 0; n < 10; ++n) {
a[n] = '\0';
}
}
void reverse(int* a) {
for (int n = 0; n < 10; ++n) {
int tmp = a[n];
a[n] = a[9 - n];
a[9 - n] = tmp;
}
}
int main(){
int a[10] = {0,1,2,3,4,5,6,7,8,9};
srand((unsigned)time(NULL));
int n;
printf("请输入您所需要的操作命令:\n 1 初始化数组\n 2 清空数组\n 3 数组元素逆置\n");
scanf("%d", &n);
switch (n) {
case 1:
init(a);//初始化数组
for (int i = 0; i < 10; ++i) {
printf("%d ",a[i] );
}
putchar('\n');
break;
case 2:
init(a);
for (int i = 0; i < 10; ++i) {
printf("%d ", a[i]);
}
putchar('\n');
empty(a);//清空数组
for (int i = 0; i < 10; ++i) {
printf("%d ", a[i]);
}
putchar('\n');
break;
case 3:
init(a);
for (int i = 0; i < 10; ++i) {
printf("%d ", a[i]);
}
putchar('\n');
reverse(a);
for (int i = 0; i < 10; ++i) {
printf("%d ", a[i]);
}
putchar('\n');
break;
default:
break;
}
system("pause");
}
判断素数
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
int is_prime(int *n) {
for (int i = 2; i < *n; ++i) {
if (*n % i == 0) {
return 0;
}
if (i == *n - 1) {
return 1;
}
}
}
int main() {
int n;
printf("请输入一个需要判断的数字:");
scanf("%d", &n);
if (is_prime(&n)) {
printf("是素数\n");
} else {
printf("不是素数\n");
}
system("pause");
}