C# Program to find the sum of even and odd elements in an array
In this program, You will learn how to find the sum of even and odd elements in an array in C#.
List is: 11 12 13 14 15
Even sum is: 12 + 14 => 26
Odd sum is: 11 + 13 + 15 => 39
Example: How to find the sum of even and odd elements in an array in C#.
using System;
public class Program
{
public static void Main(string[] args)
{
int i, se = 0, sod = 0;
int[] arr = new int[5];
Console.Write("Enter five numbers:");
for (i = 0; i < arr.Length; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
for (i = 0; i < 5; i++)
{
if (arr[i] % 2 == 0)
{
se = se + arr[i];
}
else
{
sod = sod + arr[i];
}
}
Console.WriteLine("Sum of even list is:" + se);
Console.WriteLine("Sum of odd list is:" + sod);
}
}
Output:
Enter five numbers:2
3
4
5
6
Sum of even list is:12
Sum of odd list is:8