C# Program to Find the Average of an Array

This is a C# Program to compute average for the set of values.

Problem Description

This C# Program Computes Average for the Set of Values.

Problem Solution

Here the elements are obtained from the user and its average is found and displayed.

Program/Source Code

Here is source code of the C# Program to Compute Average for the Set of Values. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Compute Average for the Set of Values
 */
using System;
class program
{
    public static void Main()
    {
        int m, i, sum = 0, avg = 0;
        Console.WriteLine("Enter the Number of Terms in the Array ");
        m = int.Parse(Console.ReadLine());
        int[] a = new int[m];
        Console.WriteLine("Enter the Array Elements ");
        for (i = 0; i < m; i++)
        {
            a[i] = int.Parse(Console.ReadLine());
        }
        for (i = 0; i < m; i++)
        {
            sum += a[i];
        }
        avg = sum / m;
        Console.WriteLine("Average is {0}", avg);
        Console.ReadLine();
    }
}
Program Explanation

In this C# program, we are reading the number of terms in the array using ‘m’ variable. Then for loop is used to enter the coefficient element values of the array. Compute the values and divide the size of an array. Print the average for the set of values.

advertisement
advertisement
Runtime Test Cases
 
Enter the Number of Terms in the Array : 4
Enter the Elements
1
2
3
4
Average is 2

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.