C语言小练习3.27

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值