用C语言写一个判断倍数的函数
时间: 2024-01-21 07:16:32 浏览: 136
以下是用C语言编写的判断倍数的函数:
```c
#include <stdio.h>
// 判断num是否是factor的倍数
int is_multiple(int num, int factor) {
if (num % factor == 0) {
return 1; // 是倍数,返回1
} else {
return 0; // 不是倍数,返回0
}
}
int main() {
int num, factor;
printf("请输入一个整数num:");
scanf("%d", &num);
printf("请输入一个整数factor:");
scanf("%d", &factor);
if (is_multiple(num, factor)) {
printf("%d是%d的倍数\n", num, factor);
} else {
printf("%d不是%d的倍数\n", num, factor);
}
return 0;
}
```
运行程序,输入一个整数和一个因子,程序会输出判断结果。例如输入num为10,factor为5,程序会输出“10是5的倍数”。
相关问题
用C语言写一个最小公倍数的函数
```
#include <stdio.h>
int lcm(int a, int b);
int main()
{
int num1, num2, LCM;
printf("输入两个正整数:");
scanf("%d %d", &num1, &num2);
if (num1 <= 0 || num2 <= 0)
{
printf("输入的数字必须是正整数\n");
return 0;
}
LCM = lcm(num1, num2);
printf("最小公倍数为:%d\n", LCM);
return 0;
}
int lcm(int a, int b)
{
int max;
max = (a > b) ? a : b;
while (1)
{
if (max % a == 0 && max % b == 0)
return max;
++max;
}
}
```
这个函数接收两个正整数作为参数,并返回它们的最小公倍数。它首先检查输入数据是否是正整数,然后使用一个循环来计算最小公倍数。
C语言编写一个判断函数,求两个整数的最大公约数和最小公倍数
好的,可以为您解答。以下是求两个整数的最大公约数和最小公倍数的代码:
```c
#include <stdio.h>
int gcd(int a, int b) { // 求最大公约数
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int lcm(int a, int b) { // 求最小公倍数
return a * b / gcd(a, b);
}
int main() {
int num1, num2;
printf("请输入两个整数:");
scanf("%d %d", &num1, &num2);
printf("%d和%d的最大公约数为%d,最小公倍数为%d\n", num1, num2, gcd(num1, num2), lcm(num1, num2));
return 0;
}
```
希望能帮到您。如果您有其他问题,可以继续问我。
阅读全文
相关推荐














