Program to Calculate Average of Multiple Numbers Using Arrays in CPP (C plus plus)
In this tutorial, we will learn about theĀ Program to Calculate Average of Multiple Numbers Using Arrays in CPP (C plus plus).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<span style="font-size: 14pt; font-family: arial, helvetica, sans-serif;">#include <iostream> using namespace std; int main() { float avg; int no; int k; float add=0.0; float num[100]; cout<<"Please enter the no. of data: "; cin>>no; while(no>100||no<=0) { cout<<"Error!"<<"Entered number should be in range of [1-100]."<<endl<<endl; cout<<"Please enter the number again: "; cin>>no; } for(k=0;k<no;++k) { cout<<k+1<<". Please enter number: "; cin>>num[k]; add+=num[k]; } avg=add/no; cout<<"Average = "<<avg; return 0; }</span> |
Output: