You can also calculate average using variable number of arguments. The principle of this a function that an unknown number of arguments is stored in a stack and we can take them. And you can using this function like: double av1 = average( 5, 3.0, 1.5, 5.0, 1.0, 2.0 ); double av2 = average( 2, 3.0, 1.5 );
Keeping this in view, how do you calculate average in C++?
Average of numbers is calculated by adding all the numbers and then dividing the sum by count of numbers available.
Also Know, how do you find the average of n numbers? In simple words, to calculate the average of N numbers we have to add all the numbers, and then divide their sum by N.
In this way, how do you find the average in C Plus Plus?
Formula to compute the average of three numbers is given below.
We shall use following algorithm to compute average of three numbers in C++.
- Start.
- Read first number to num_1.
- Read second number to num_2.
- Read third number to num_3.
- Initialize average with (num_1 + num_2 + num_3) /3.
- Stop.
How do you find the average of 5 numbers in C?
Solution:
- // Program to calculate average of 5 numbers entered by users.
- {
- int num; // Declare 'num' to read number from users.
- int sum, i; // Declare variables 'sum' to keep sum of numbers & 'i' used in for loop.
- float average; // Declae variable 'average' of float type to save average value.
