C++ Program to Find Average of N Numbers using For loop | While loop | Array | Functions
C++ program to find average of N numbers using for loop, while loop, array, and functions.
In this article, you will learn how to make a C++ program to find average of N numbers using for loop, while loop, array, and functions.
Example
Enter the number of elements to calculate the average::
4
Enter 4 elements one by one
6462
62
5645
667
The average of the entered input numbers is = 3209
You should have the knowledge of the following topics in c++ programming to understand these programs:
- C++
main()
function - C++
cin
object - C++
cout
object - C++ For loop
- C++ While loop
- C++ Array
- C++ Functions
In this article, we solve this problem in five methods:
- Using the for loop
- Using the while loop
- Using the array
- Using the functions
- Using the friend function
Source Code
// C++ Program to Find Average of N Numbers using For loop
#include <iostream>
using namespace std;
int main() {
int x, i;
float avg = 0, y;
// x - To store the number of elements
// avg - To store the total average value
// y - To store the total input numbers
cout << "Enter the number of elements to calculate the average::\n";
cin >> x;
cout << "Enter " << x << " elements one by one\n\n";
for(i = 0; i < x; i++) {
cin >> y;
avg += y;
}
avg /= x;
cout << "\nThe average of the entered input numbers is = " << avg;
return 0;
}
Output
Enter the number of elements to calculate the average::
4
Enter 4 elements one by one
6462
62
5645
667
The average of the entered input numbers is = 3209
Explanation
In this given program, we have taken two types of input from the user first size of the number of elements 4
and second, these numbers following: 6462
, 62
, 5645
, and 667
to calculate the average on these numbers via the system console.
Then we make sum of these numbers and it is divided by the size of element 4.
After the above calculation, it will return 3209
average of the given input numbers.
C++ Program to Find Average of N Numbers using While loop
// C++ Program to Find Average of N Numbers using While loop
#include <iostream>
using namespace std;
int main() {
int x, i = 0;
float avg = 0, y;
// x - To store the number of elements
// avg - To store the total average value
// y - To store the total input numbers
cout << "Enter the number of elements to calculate the average::\n";
cin >> x;
cout << "Enter " << x << " elements one by one\n\n";
while (i < x) {
cin >> y;
avg += y;
i++;
}
avg /= x;
cout << "\nThe average of the entered input numbers is = " << avg;
return 0;
}
C++ Program to Find Average of N Numbers using Array
// C++ Program to Find Average of N Numbers using Array
#include <iostream>
using namespace std;
int main() {
int x, i;
float avg = 0.00;
// x - To store the number of elements
// avg - To store the total average value
// y - To store the total input numbers
cout << "Enter the number of elements to calculate the average::\n";
cin >> x;
float Num[x];
cout << "\nEnter " << x << " elements one by one\n";
for(i = 0; i < x; i++) {
cin >> Num[i];
avg += Num[i];
}
avg /= x;
cout << "\nAverage of the entered input numbers is = " << avg;
return 0;
}
Output
Enter the number of elements to calculate the average::
5
Enter 5 elements one by one
12
43
65
78
90
Average of the entered input numbers is = 57.6
C++ Program to Find Average of N Numbers using Functions
// C++ Program to Find Average of N Numbers using Functions
#include <iostream>
using namespace std;
// This function will calculate average of N numbers
float FindAverage(int x) {
int i;
float avg = 0, y;
// avg - To store the total average value
// y - To store the total input numbers
cout << "\nEnter " << x << " elements one by one\n\n";
for(i = 0; i < x; i++) {
cin >> y;
avg += y;
}
avg /= x;
cout << "\nThe average of the entered input numbers is = " << avg;
}
// It's the driver function
int main() {
int x; // To store the number of elements
cout << "Enter the number of elements to calculate the average::\n";
cin >> x;
FindAverage(x);
return 0;
}
C++ Program to Find Average of N Numbers using Friend Function
// C++ Program to Find Average of N Numbers using Friend Function
#include <bits/stdc++.h>
using namespace std;
class Average {
public:
int x; // To store the number of elements
void Display() {
cout << "Enter the number of elements to calculate the average::\n";
cin >> x;
}
// It will print the final output
friend void Calculate(Average AR);
};
void Calculate(Average AR) {
float avg = 0, y;
// avg - To store the total average value
// y - To store the total input numbers
cout << "\nEnter " << AR.x << " elements one by one\n";
for(int i = 0; i < AR.x; i++) {
cin >> y;
avg += y;
}
avg /= AR.x;
cout << "\nThe average of the entered input numbers is = " << avg << endl;
}
// It's the driver function
int main() {
// To create the instance of `Average` class
Average AR;
AR.Display();
// It will print the final output
Calculate(AR);
return 0;
}
Output
Enter the number of elements to calculate the average::
5
Enter 5 elements one by one
23
45
76
134
872
The average of the entered input numbers is = 230
Also, visit these links
C Program to Find Factors of a Number