Wednesday, 14 January 2015

Cpp Program to find the average of all the elements in an array


This is a Cpp Program which finds the average of all the elements in an array.

Here we calculate the average by  taking sum of all the elements in the array and dividing them by the total number of elements in an array.

PROGRAM :
#include <iostream>

using namespace std;

int main()
{
    int i,n;
    float sum = 0,average;
    cout << "\nEnter the no of Elements: \t";
    cin >> n;
    int arr[n];
    for(i=0;i<n;i++)
    {
        cout << "\nEnter Element " <<  i+1 << ": \t";
        cin >> arr[i];
    }
    for(i=0;i<n;i++)
    sum=sum+arr[i];
    
    average=sum/n;
    cout << "\nThe average of "<< n <<"  numbers you entered is   :"<< average;
    return 0;
}

OUTPUT : 
Cpp - Average of all the elements in an array

No comments:

Post a Comment