How do you calculate average percentage in C++?
![]() |
How do you calculate average percentage in C++? |
In C++, calculating the average percentage is a simple task. However, it requires a basic understanding of arithmetic operations, loops, and data types. In this article, we will discuss how to calculate the average percentage in C++ and the code snippet to perform the calculation.
The average percentage is the arithmetic mean of a set of percentages. To calculate the average percentage, we need to sum up all the percentages and divide the sum by the number of percentages. The result will be the average percentage.
Let's take an example to understand it better. Suppose we have a class of ten students, and their percentages are as follows:
80%, 85%, 90%, 75%, 70%, 60%, 50%, 55%, 65%, 95%
To calculate the average percentage of the class, we need to sum up all the percentages and divide the sum by the number of percentages, i.e., 10.
The sum of all the percentages is:
80+85+90+75+70+60+50+55+65+95 = 715
Now, we will divide the sum by the number of percentages, i.e., 10, to get the average percentage:
715/10 = 71.5
So, the average percentage of the class is 71.5%.
Now, let's move on to the C++ code to calculate the average percentage.
The first step is to declare an array to store the percentages of the students. In this example, we will declare an array named 'percentage' of size 10 to store the percentages.
int percentage[10];
The next step is to initialize the array with the percentage values of the students. We can either initialize the array at the time of declaration or assign the values later.
int percentage[10] = {80, 85, 90, 75, 70, 60, 50, 55, 65, 95};
Once we have the array of percentages, we can calculate the sum of all the percentages using a loop. We will use a 'for' loop to iterate over the array and add each percentage to the sum.
C++
int sum = 0;
for(int i=0; i<10; i++)
{
sum += percentage[i];
}
In the above code snippet, we have initialized the variable 'sum' with zero, and then we have used a 'for' loop to iterate over the array. The loop will run from i=0 to i<10, i.e., from the first element of the array to the last element of the array. In each iteration, the loop will add the value of percentage[i] to the sum.
Once we have the sum of all the percentages, we can calculate the average percentage by dividing the sum by the number of percentages, i.e., 10.
float averagePercentage = (float)sum/10;
In the above code snippet, we have declared a variable 'averagePercentage' of type float to store the average percentage. We have cast the sum to a float type to ensure that the division operation results in a floating-point value. Finally, we have divided the sum by 10 to get the average percentage.
We can then print the value of the average percentage using the 'cout' statement
cout << "The average percentage is: " << averagePercentage << "%" << endl;
The final code to calculate the average percentage of the class is as follows:
#include<iostream>
using namespace std;
int main()
{
int percentage[10] = {80, 85, 90, 75, 70, 60, 50,
0 Comments