top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

If given an array of ints, how to write a C# method to total all the values that are even numbers?

0 votes
847 views
If given an array of ints, how to write a C# method to total all the values that are even numbers?
posted May 9, 2017 by Madhavi Kumari

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes
namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {

        //variable asked from user haw much elements in array
        Console.WriteLine("haw much number voud you like to input");    
        int haw_much = Convert.ToInt32(Console.ReadLine());

        // array created with numbers of spaces which user inputed 
        int[] numbers = new int[haw_much];

        // loop to input your numbers 
        //.Count() gives you number of elements in array
        // only one line of code in loop 
        for (int i = 0; i != numbers.Count(); i++)
        numbers[i] =Convert.ToInt32(Console.ReadLine());


        // the sum value sum of even number
        // and loop for calculation 
        int sum = 0;
        for (int i = 0; i != numbers.Count(); i++)
        if (numbers[i] % 2 == 0) { sum += numbers[i]; }


        // display of sum of even numbers 
        Console.WriteLine("the sum of even numbers in array is {0} ", sum);

        Console.ReadLine();
       }
   }
}
answer May 11, 2017 by Leon Martinović
...