C语言计算数字能否被3个数整除 编程实现:输入- -个整数,判断它能否被3,5, 7整除,并输出以下信息之一-:①能同时被3, 5, 7整除;②能被其中两数(要指出哪两个)整除;③能被其中一个数(要指出哪一个)整除;④不能被3,5, 7任一个整除。
时间: 2024-10-27 08:06:30 浏览: 56
在C语言中,你可以通过编写一个函数来实现这个功能。首先,我们需要获取用户的输入,然后检查这个数字是否能被3、5和7整除。这里是一个简单的示例:
```c
#include <stdio.h>
// 函数声明
int isDivisible(int num, int divisors[], int count);
int main() {
int number;
printf("请输入一个整数: ");
scanf("%d", &number);
// 定义要检查的数字列表
int divisors[] = {3, 5, 7};
int count = sizeof(divisors) / sizeof(divisors[0]);
int result = isDivisible(number, divisors, count);
switch (result) {
case 0:
printf("能同时被3, 5, 7整除.\n");
break;
case 1:
printf("能被%d和%d整除.\n", divisors[0], divisors[1]);
break;
case 2:
printf("能被%d整除.\n", divisors[result - 2]);
break;
default:
printf("不能被3, 5, 7任一数整除.\n");
break;
}
return 0;
}
// 判断函数
int isDivisible(int num, int divisors[], int count) {
for (int i = 0; i < count; i++) {
if (num % divisors[i] == 0)
return count - i; // 返回剩余未被整除的数对应的索引
}
return count; // 如果都不能整除,返回count表示无匹配
}
```
在这个程序中,`isDivisible`函数会检查输入的数字`num`是否可以被`divisors`数组中的每个元素整除,并返回剩下的未整除的数的数量作为指示符。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)