C++ Program to Find Average of Array Elements

Write C++ Program to Find Average of Array Elements

// CPP Program to Find Average of Array Elements

#include <iostream>

using namespace std;

int main()
{
	int A[] = {1, 2, 3, 4, 5, 6, 7, 8}, i;
	int n = sizeof(A) / sizeof(A[0]);

    cout << "Array Elements :--> ";
	for(i=0; i<n; i++)
        cout << A[i] << " ";

	float sum = 0;

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

	cout << "\n\nNumber of Array Elements :--> " << n;
	cout << "\nSum of Array Elements :--> " << sum;
	cout << "\nAverage of Array Elements :--> " << (sum/n);
	return 0;
}

Output:

Array Elements :--> 1  2  3  4  5  6  7  8

Number of Array Elements :--> 8
Sum of Array Elements :--> 36
Average of Array Elements :-->  4.50

≪ Previous | Next ≫