average a fixed array of numbers using a loop. - CSharp Language Basics

CSharp examples for Language Basics:Array

Description

average a fixed array of numbers using a loop.

Demo Code

using System;//  w w  w . j  av a 2  s. c o m
public class Program
{
   public static void Main(string[] args)
   {
      double[] array = {5, 2, 7, 3.5, 6.5, 8, 1, 9, 1, 3};
      double sum = 0;
      for (int i = 0; i < 10; i++)
      {
         sum = sum + array[i];
      }
      // Now calculate the average.
      double average = sum / 10;
      Console.WriteLine(average);
   }
}

Result


Related Tutorials