- Related Questions & Answers
- Java program to find the average of given numbers using arrays
- Java program to calculate the average of numbers in Java
- Calculate average of numbers in a column MySQL query?
- C++ Program to Calculate Sum of Natural Numbers
- 8086 program to find average of n numbers
- MongoDB query to calculate average
- Java program to calculate mean of given numbers
- Average of a stream of numbers in C++
- C++ Program to Calculate Power Using Recursion
- Java program to calculate the product of two numbers
- Calculate average value in MongoDB
- C++ program to Calculate Factorial of a Number Using Recursion
- Average of Squares of Natural Numbers?
- PHP program to calculate the repeated subtraction of two numbers
- How to calculate average of a particular field in MongoDB?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Average of numbers is calculated by adding all the numbers and then dividing the sum by count of numbers available.

An example of this is as follows.

The numbers whose average is to be calculated are: 10, 5, 32, 4, 9 Sum of numbers = 60 Average of numbers = 60/5 = 12

A program that calculates average of numbers using arrays is as follows.

#include <iostream> using namespace std; int main() { int n, i; float sum = 0.0, avg; float num[] = {12, 76, 23, 9, 5}; n = sizeof(num) / sizeof(num[0]); for(i = 0; i < n; i++) sum += num[i]; avg = sum / n; cout<<"Average of all array elements is "<<avg; return 0; }

Average of all array elements is 25

In the above program, the numbers whose average is needed are stored in an array num[]. First the size of the array is found. This is done as shown below −

n = sizeof(num) / sizeof(num[0]);

Now a for loop is started from 0 to n-1. This loop adds all the elements of the array. The code snippet demonstrating this is as follows.

for(i = 0; i < n; i++) sum += num[i];

The average of the numbers is obtained by dividing the sum by n i.e amount of numbers. This is shown below −

avg = sum / n;

Finally the average is displayed. This is given as follows.

cout<<"Average of all array elements is "<<avg;

Advertisements