Ques:
Print the Sum of Even and Odd Number Seperatly of an Initialised Array in C#.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Even_Odd_Array_Sepration
{
class Program
{
static void Main(string[] args)
{
int odd = 0, even = 0, i;
int[] a = new int[] {1,2,3,4,5,6};
for (i = 0; i < a.Length; i++)
{
if (a[i] % 2 == 0)
{
even = even + a[i];
}
else
{
odd = odd + a[i];
}
}
Console.Write("Sum of Even Number is : " + even);
Console.Write("Sum of Odd Number is : " + odd);
Console.ReadKey();
}
}
}
If you learned from this post share with others. Happy Sharing!
0 Comments