将1-200中所有能被6整除、但不能被4整除的数存到一个一维数组中,用c语言
时间: 2023-11-12 18:07:55 浏览: 37
下面是用 C 语言实现的代码:
```c
#include <stdio.h>
#define SIZE 33
int main() {
int arr[SIZE];
int count = 0;
for (int i = 1; i <= 200; i++) {
if (i % 6 == 0 && i % 4 != 0) {
arr[count] = i;
count++;
}
}
printf("Numbers divisible by 6 but not by 4 in the range of 1 to 200:\n");
for (int i = 0; i < count; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
这里定义了一个 `SIZE` 常量为 33,是因为在 1 到 200 中,能被 6 整除但不能被 4 整除的数最多有 33 个,所以我们定义数组的大小为 33。
然后使用一个 for 循环遍历 1 到 200 的所有整数,如果该整数能被 6 整除但不能被 4 整除,则将其存入数组中,并将计数器 `count` 加 1。
最后再使用一个 for 循环输出数组中存储的所有元素。
相关问题
c语言编写将1-200中所有能被6整除但不能被4整除的数存到一个一维数组中并输出
下面是使用C语言编写的将1-200中所有能被6整除但不能被4整除的数存到一个一维数组中并输出的代码:
```c
#include <stdio.h>
#define MAX_SIZE 50
int main() {
int arr[MAX_SIZE], i, j, count = 0;
for (i = 1; i <= 200; i++) {
if (i % 6 == 0 && i % 4 != 0) {
arr[count] = i;
count++;
}
}
printf("The numbers that meet the requirements are: \n");
for (j = 0; j < count; j++) {
printf("%d ", arr[j]);
}
printf("\n");
return 0;
}
```
上述代码中,首先定义了一个大小为50的一维数组`arr`,用于存储符合要求的数。接着使用for循环从1到200遍历每个数,判断当前数是否能被6整除但不能被4整除,如果是,则将其存入数组中。最后使用另一个for循环遍历数组并输出其中的数。
运行上述代码,输出结果如下:
```
The numbers that meet the requirements are:
6 18 30 42 54 66 78 90 102 114 126 138 150 162 174 186 198
```
可以看到,输出了1-200中所有能被6整除但不能被4整除的数。
将1-200中所有能被6整除、但不能被4整除的数存到一个一维数组中,并输出。C语言
以下是将1-200中所有能被6整除、但不能被4整除的数存到一个一维数组中,并输出的C语言代码:
```c
#include <stdio.h>
int main() {
int arr[100], count = 0;
for (int i = 1; i <= 200; i++) {
if (i % 6 == 0 && i % 4 != 0) {
arr[count++] = i;
}
}
printf("能被6整除但不能被4整除的数:\n");
for (int i = 0; i < count; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
运行结果:
```
能被6整除但不能被4整除的数:
6 18 30 42 54 66 78 90 102 114 126 138 150 162 174 186 198
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)