c语言数组输入n个整数,输出偶数
时间: 2024-09-30 18:08:08 浏览: 63
在C语言中,如果你想输入n个整数并只输出其中的偶数,你可以按照以下步骤编写程序:
```c
#include <stdio.h>
int main() {
int n, num;
printf("请输入数字的数量(n): ");
scanf("%d", &n);
// 创建一个数组来存储输入的整数
int arr[n];
// 输入n个整数到数组
for (int i = 0; i < n; i++) {
printf("请输入第 %d 个数: ", i + 1);
scanf("%d", &arr[i]);
}
// 遍历数组,检查并打印偶数
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) { // 检查是否是偶数
printf("%d 是偶数\n", arr[i]);
}
}
return 0;
}
```
当你运行这个程序,它会提示用户输入数字的数量和每个数字,然后筛选出并输出其中的偶数。
相关问题
C语言数组奇偶排序(数组) 题目描述 输入n,输入n个整数。调整数字位置使得偶数在序列的前半部分,奇数在序列的后半部分。 注:自己根据样例输入、输出找到算法。 要求算法的时间复杂度为O(n)。 输入 测试次数t 每组测试数据格式如下: 整数个数n,后跟n个整数 输出 将每组整数个和数测试数据作为一个序列,输出整数后的序列。
好的,这是一个比较简单的问题,我可以给你提供一个 C 语言的实现,时间复杂度为 O(n)。代码如下:
```c
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void sort(int arr[], int n) {
int left = 0; // 左指针指向序列的开头
int right = n - 1; // 右指针指向序列的结尾
while (left < right) {
while (left < right && arr[left] % 2 == 0) {
left++;
}
while (left < right && arr[right] % 2 != 0) {
right--;
}
if (left < right) {
swap(&arr[left], &arr[right]);
}
}
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
sort(arr, n);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
return 0;
}
```
这段代码首先读入测试数据的组数 t,然后对于每组测试数据,读入整数个数 n 和 n 个整数,然后调用 `sort()` 函数对整数序列进行排序,最后输出排序后的整数序列。
在 `sort()` 函数中,我们使用了双指针的思想,左指针指向序列的开头,右指针指向序列的结尾。当左指针指向的数是偶数时,左指针往右移动一位;当右指针指向的数是奇数时,右指针往左移动一位;如果此时左指针指向的数是奇数,右指针指向的数是偶数,我们就交换这两个数。这样做的时间复杂度是 O(n)。
C语言数组实现:从键盘输入 10 个整数存入数组中,统计所有数组元素的值为偶数的个数的代码
好的,下面是实现的C语言代码:
```c
#include <stdio.h>
int main() {
int arr[10];
int count = 0;
// 从键盘输入10个整数存入数组中
for (int i = 0; i < 10; i++) {
printf("请输入第%d个整数:", i+1);
scanf("%d", &arr[i]);
}
// 统计数组元素的值为偶数的个数
for (int i = 0; i < 10; i++) {
if (arr[i] % 2 == 0) {
count++;
}
}
// 输出结果
printf("数组中值为偶数的元素个数为:%d\n", count);
return 0;
}
```
运行结果:
```
请输入第1个整数:1
请输入第2个整数:2
请输入第3个整数:3
请输入第4个整数:4
请输入第5个整数:5
请输入第6个整数:6
请输入第7个整数:7
请输入第8个整数:8
请输入第9个整数:9
请输入第10个整数:10
数组中值为偶数的元素个数为:5
```
希望对您有帮助!
阅读全文
相关推荐
![c](https://img-home.csdnimg.cn/images/20250102104920.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://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)