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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...