用c语言定义一个有10个元素的一维数组,输入10个数存入数组中,输出能被3或7整除的数
时间: 2024-05-15 13:16:39 浏览: 10
```c
#include <stdio.h>
int main() {
int arr[10];
int i;
// 输入10个数
printf("请输入10个整数:\n");
for (i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
// 输出能被3或7整除的数
printf("能被3或7整除的数有:");
for (i = 0; i < 10; i++) {
if (arr[i] % 3 == 0 || arr[i] % 7 == 0) {
printf("%d ", arr[i]);
}
}
return 0;
}
```
相关问题
C语言随机函数找出其中能被3或5整除的数,存储到另一个一维数组中,并输出
好的,以下是代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 10 // 数组大小
int main()
{
int arr[SIZE], res[SIZE]; // 定义原数组和结果数组
int count = 0; // 计数器
srand(time(NULL)); // 初始化随机数种子
// 生成随机数并存入原数组
for (int i = 0; i < SIZE; i++) {
arr[i] = rand() % 100 + 1; // 生成1-100之间的随机数
printf("%d ", arr[i]); // 输出原数组中的元素
if (arr[i] % 3 == 0 || arr[i] % 5 == 0) { // 如果能被3或5整除
res[count++] = arr[i]; // 存入结果数组中
}
}
printf("\nResult:\n");
// 输出结果数组中的元素
for (int i = 0; i < count; i++) {
printf("%d ", res[i]);
}
return 0;
}
```
运行结果类似如下:
```
68 22 36 61 90 92 91 51 23 50
Result:
36 90 51 50
```
其中,原数组中的元素是随机生成的,每次运行结果都可能不同。而结果数组中只有能被3或5整除的数。
c语言写一个代码:300到500能被7和17整除的数放到一维数组中
以下是一个示例代码:
```
#include <stdio.h>
int main() {
int a[100], b = 0; // 定义一维数组a和计数器b
for (int i = 300; i <= 500; i++) { // 从300遍历到500
if (i % 7 == 0 && i % 17 == 0) { // 判断是否能被7和17整除
a[b] = i; // 如果能,将该数存入数组a
b++; // 计数器b加1
}
}
printf("能被7和17整除的数共有%d个:\n", b); // 输出能被7和17整除的数的个数
for (int i = 0; i < b; i++) { // 遍历数组a
printf("%d ", a[i]); // 输出数组a中的每一个数
}
return 0;
}
```
相关推荐
![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)