How to find the average of int array in C#

1 Answer

0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = { 1, 2, 3, 4, 5, 6 };
            float sum = 0;

            for (int i = 0; i < arr.Length; i++)
                sum += arr[i];

            Console.WriteLine("average = " + sum / arr.Length);
        }
    }
}

/*
run:
   
average = 3.5
  
*/

 



answered Feb 11, 2016 by avibootz
edited Feb 17, 2016 by avibootz

Related questions

1 answer 154 views
1 answer 191 views
2 answers 57 views
1 answer 143 views
1 answer 156 views
...