如何使用 For 循环、While 循环、函数编写 C 语言程序来查找数组中奇偶数的和,并附带示例。
C 语言查找数组中奇偶数之和的程序
此程序允许用户输入一维数组的大小和行元素。
/* C Program to find Sum of Even and Odd Numbers in an Array */
#include<stdio.h>
int main()
{
int Size, i, a[10];
int Even_Sum = 0, Odd_Sum = 0;
printf("\n Please Enter the Size of an Array : ");
scanf("%d", &Size);
printf("\nPlease Enter the Array Elements\n");
for(i = 0; i < Size; i++)
{
scanf("%d", &a[i]);
}
for(i = 0; i < Size; i ++)
{
if(a[i] % 2 == 0)
{
Even_Sum = Even_Sum + a[i];
}
else
{
Odd_Sum = Odd_Sum + a[i];
}
}
printf("\n The Sum of Even Numbers in this Array = %d ", Even_Sum);
printf("\n The Sum of Odd Numbers in this Array = %d ", Odd_Sum);
return 0;
}

在这里,For 循环将确保数字在 0 和最大值之间。在此示例中,它将从 0 到 4。
for(i = 0; i < Size; i ++)
在下一行,我们声明了If语句
if(a[i] % 2 == 0)
任何能被 2 整除的数都是偶数。If 语句将检查当前 一维数组元素除以 2 的余数是否恰好等于 0。
- 如果条件为真,则为偶数,编译器将把数组元素添加到 Even_Sum。
- 如果条件为假,则为奇数。然后编译器将把数组元素添加到 Odd_Sum。
使用 While 循环查找数组中奇偶数之和的程序
这个程序与上面的相同,但这次我们使用了 While 循环来对数组中的奇偶数进行求和。
/* C Program to find Sum of Even and Odd Numbers in an Array */
#include<stdio.h>
int main()
{
int Size, i, j = 0, a[10];
int Even_Sum = 0, Odd_Sum = 0;
printf("\n Please Enter the Size of an Array : ");
scanf("%d", &Size);
printf("\nPlease Enter the Array Elements\n");
for(i = 0; i < Size; i++)
{
scanf("%d", &a[i]);
}
while(j < Size)
{
if(a[j] % 2 == 0)
{
Even_Sum = Even_Sum + a[j];
}
else
{
Odd_Sum = Odd_Sum + a[j];
}
j++;
}
printf("\n The Sum of Even Numbers in this Array = %d ", Even_Sum);
printf("\n The Sum of Odd Numbers in this Array = %d ", Odd_Sum);
return 0;
}
Please Enter the Size of an Array : 6
Please Enter the Array Elements
14 26 53 19 89 156
The Sum of Even Numbers in this Array = 196
The Sum of Odd Numbers in this Array = 161
使用函数查找数组中奇偶数之和的程序
这个程序与第一个C 编程示例相同。但是,我们使用函数将查找偶数之和与查找奇数之和的逻辑分开了。
/* C Program to Count Even and Odd Numbers in an Array */
#include<stdio.h>
int CountEvenNumbers(int a[], int Size);
int CountOddNumbers(int a[], int Size);
int main()
{
int Size, i, a[10];
int Even_Count = 0, Odd_Count = 0;
printf("\n Please Enter the Size of an Array\n");
scanf("%d", &Size);
printf("\nPlease Enter the Array Elements : ");
for(i = 0; i < Size; i++)
{
scanf("%d", &a[i]);
}
Even_Count = CountEvenNumbers(a, Size);
Odd_Count = CountOddNumbers(a, Size);
printf("\n Total Number of Even Numbers in this Array = %d ", Even_Count);
printf("\n Total Number of Odd Numbers in this Array = %d ", Odd_Count);
return 0;
}
int CountEvenNumbers(int a[], int Size)
{
int i, Even_Count = 0;
printf("\n List of Even Numbers in this Array: ");
for(i = 0; i < Size; i ++)
{
if(a[i] % 2 == 0)
{
printf("%d ", a[i]);
Even_Count++;
}
}
return Even_Count;
}
int CountOddNumbers(int a[], int Size)
{
int i, Odd_Count = 0;
printf("\n List of Odd Numbers in this Array: ");
for(i = 0; i < Size; i ++)
{
if(a[i] % 2 != 0)
{
printf("%d ", a[i]);
Odd_Count++;
}
}
return Odd_Count;
}
Please Enter the Size of an Array
10
Please Enter the Array Elements : 10 15 25 69 78 96 47 57 94 109
List of Even Numbers in this Array: 10 78 96 94
List of Odd Numbers in this Array: 15 25 69 47 57 109
Total Number of Even Numbers in this Array = 4
Total Number of Odd Numbers in this Array = 6