top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
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
243 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ć
Similar Questions
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...