Average of array elements || Arrays || C++

In this, we are going to print the average of the array element in  C++.

The code given below can be used for TURBO C++ Compiler:-

#include <iostream.h>
#include <conio.h>
 
void main() 
{
    clrscr();
    int i,sum = 0, a[5] = {1,3,5,7,9};
    float average;
    for(i = 0; i < 5; i++)
    {
        sum += a[i];
    }
    average = sum / 5;
    cout << "Average = " << average;
 
    getch();
}
  
  

The code given below can be used for g++/gcc Compiler:-

#include <iostream>
using namespace std;
 
int main() 
{
    int i,sum = 0, a[5] = {1,3,5,7,9};
    float average;
    for(i = 0; i < 5; i++)
    {
        sum += a[i];
    }
    average = sum / 5;
    cout << "Average = " << average;
 
    return 0;
}

#ENJOY CODING


Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post