C# Program to Find the Average of All the Array Elements


This is a C# Program to find the average values of all the array elements.

Problem Description

This C# Program Finds the Average Values of all the Array Elements.

Problem Solution

Here the array elements are obtained from the user and the sum is first calculated. Average is then found by dividing the sum by the number of terms.

Program/Source Code

Here is source code of the C# Program to Find the Average Values of all the Array Elements. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

 * C# Program to Find the Average Values of all the Array Elements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class program
    public void sumAverageElements(int[] arr, int size)
        int sum = 0;
       int average = 0;
        for (int i = 0; i < size; i++)
            sum += arr[i];
        average = sum / size; 
        Console.WriteLine("Sum Of Array is : " + sum);
        Console.WriteLine("Average Of Array is : " + average);
    public static void Main(string[] args)
        int size;
        Console.WriteLine("Enter the Size :");
        size = Convert.ToInt32(Console.ReadLine());
        int[] a = new int[size];
        Console.WriteLine("Enter the Elements of the Array : ");
        for (int i = 0; i < size; i++)
            a[i] = Convert.ToInt32(Console.ReadLine());
        int len = a.Length;
        program pg = new program();
        pg.sumAverageElements(a, len);
Program Explanation

In this C# program, we are reading the size of an array using ‘size’ variable. Using for loop we are entering the coefficient values of an array.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

The sumAverageElements() function is used to compute the sum of coefficient value. Find the value of ‘average’ variable by dividing the value of ‘sum’ variable by the value of ‘size’ variable. Print the average values of all the array elements.

Runtime Test Cases
Enter the Size : 
Enter the Elements of the Array :
Sum of the Array is : 150
Average of the Array is : 30

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

Check this: MCA Books | C# Books
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

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.