c语言将1-200中所有能被6整除、但不能被4整除的数存到一个一维数组中,并输出。
时间: 2023-10-25 11:08:50 浏览: 39
```c
#include <stdio.h>
#define SIZE 34
int main() {
int arr[SIZE];
int index = 0;
for (int i = 1; i <= 200; i++) {
if (i % 6 == 0 && i % 4 != 0) {
arr[index] = i;
index++;
}
}
for (int i = 0; i < SIZE; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
其中,我们用 `#define` 定义了数组的大小为 34,因为在 1-200 中,能被 6 整除且不能被 4 整除的数一共有 34 个,这样刚好可以把它们全部存储在数组中。
然后,我们使用一个 `for` 循环将符合条件的数存储到数组中。最后,再使用一个 `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整除的数。
c语言将1-200中所有能被6整除、但不能被4整除的数存到一个一维数组中,并输出
下面是实现该功能的C语言代码:
```c
#include <stdio.h>
int main() {
int arr[50]; // 定义一个最多能存放50个数的数组
int count = 0; // 统计符合条件的数的个数
for (int i = 1; i <= 200; i++) {
if (i % 6 == 0 && i % 4 != 0) {
arr[count] = i;
count++;
}
}
printf("符合条件的数有%d个,它们分别是:\n", count);
for (int i = 0; i < count; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
代码说明:
1. 首先定义一个长度为50的一维数组,用于存放符合条件的数;
2. 用一个循环遍历1到200之间的每个数,判断该数是否能被6整除但不能被4整除;
3. 如果符合条件,则将该数存入数组中,并将计数器加1;
4. 最后输出符合条件的数的个数以及每个数的值。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)