C语言找出可以被3整除
时间: 2023-08-04 22:35:01 浏览: 74
好的,我可以帮你解决这个问题。以下是一个简单的 C 代码示例,可以找出一组数字中可以被3整除的数字:
```c
#include <stdio.h>
int main() {
int num_array[] = { 2, 5, 3, 6, 9, 8, 12, 15 };
int i, n;
n = sizeof(num_array) / sizeof(num_array[0]);
printf("Numbers divisible by 3 are: ");
for (i = 0; i < n; i++) {
if (num_array[i] % 3 == 0) {
printf("%d ", num_array[i]);
}
}
printf("\n");
return 0;
}
```
在这个例子中,我们首先定义了一个整数数组 `num_array`,其中包含了一组数字。我们计算了数组的大小,然后使用一个 `for` 循环遍历数组中的每个元素。在循环中,我们使用 `%` 运算符来检查每个数字是否可以被3整除,如果可以,就将它打印出来。最后,我们打印出所有可以被3整除的数字的列表。
希望这可以回答你的问题!
相关问题
c语言找出数组中可以被其中元素整除的元素
您好,您可以使用以下代码来找出数组中可以被其中元素整除的元素:
```
#include <stdio.h>
int main() {
int arr[] = {2, 4, 5, 6, 7, 10, 12};
int n = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < n; i++) {
int flag = 0;
for (int j = 0; j < n; j++) {
if (arr[j] != arr[i] && arr[j] % arr[i] == 0) {
flag = 1;
break;
}
}
if (flag) {
printf("%d is divisible by at least one other element in the array.\n", arr[i]);
}
}
return 0;
}
```
希望能够帮到您!
c语言找出1000以内能够整除7和2的所有数,并5个一行输出
好的,以下是c语言实现找出1000以内能够整除7和2的所有数,并5个一行输出的代码:
```c
#include <stdio.h>
int main(void)
{
int i, count = 0;
for (i = 1; i <= 1000; i++) {
if (i % 7 == 0 && i % 2 == 0) {
printf("%d ", i);
count++;
if (count % 5 == 0) { // 每输出5个数换一行
printf("\n");
}
}
}
printf("\n");
return 0;
}
```
以上代码中,使用for循环遍历1~1000的所有数,对每个数判断是否能够整除7和2,如果能够整除则输出,并使用count变量计数,每输出5个数就换一行。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.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://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)