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

In this task, we will write a C# program to find the average of all the elements in an array. An array is a collection of variables of the same type that are referenced by a common name.

Problem Statement

Write a C# program to find the average of all the elements in an array. The program should take input from the user in the form of an array of integers. The program should display the average of the array elements as output.

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

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter the number of elements in the array: ");
        int n = int.Parse(Console.ReadLine());

        int[] arr = new int[n];
        Console.WriteLine("Enter the elements of the array: ");
        for (int i = 0; i < n; i++)
        {
            arr[i] = int.Parse(Console.ReadLine());
        }

        int sum = 0;
        for (int i = 0; i < n; i++)
        {
            sum += arr[i];
        }

        double average = (double)sum / n;
        Console.WriteLine("The average of the array elements is {0}.", average);
    }
}

Input / Output

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...