In: Computer Science
C++
employees entering data for a report (or entering into a db, etc.)
this section of code will also calculate their average salary of all the entered salary amounts.
This example will use a for loop for a user given number of employees, and a nested do..while loop to enter salary information. Then find the average salary for all the entered values.
C++ program to find the average salary of employees.
Steps:
Source code:
#include<iostream>
using namespace std;
 
int main()
{
    int i,n,x,sum=0;
    float avg;
 
    cout<<"How many numbers of salaries have to entered : ";
    cin>>n;
    
 
 
    for(i=1;i<=n;++i)
    {
        cout<<"\nSalaries "<<i<<" :: ";
        cin>>x;
 
        sum+=x;
    }
 
    avg=(float)sum/(float)n;
 
    cout<<"\n\nSum of "<<n<<" salary :: "<<sum;
 
    cout<<"\n\nAverage of "<<n<<" total salaries :: "<<avg;
 
    cout<<"\n";
 
    return 0;
}
The output
How many number of salaries have to enter : 3
Salaries 1 :: 45000
Salaries 2 :: 15000
Salaries 3 :: 40000
Sum of 3 salary :: 100000
Average of 3 total salaries :: 33333.3