Based on the question below, write a program using :
a. For loop
b. While loop
c. Do..while loop
Calculate the salary that employee get based on number of working. Rate of the payment is RM8.00 per hour but the salary will be deducted with 10% KWSP. By using a repetition control structure, write a program to calculate the total salary of 5 employees and the average salary of the employee.
#include <iostream>
using namespace std;
const double PAYMENT_RATE = 8.0;
const double KWSF = 0.1;
const int N = 5;
void salary1(double hours[N]) {
double payment, tot_payment=0.0;
for (int i=0; i<N; i++) {
payment = (hours[i] * PAYMENT_RATE) * (1 - KWSF);
cout << i+1 << " employee salary is RM" << payment << endl;
tot_payment += payment;
}
cout << endl << "Average salary is " << tot_payment / N << endl << endl;
}
void salary2(double hours[N]) {
double payment, tot_payment=0.0;
int i=0;
while(i<N) {
payment = (hours[i] * PAYMENT_RATE) * (1 - KWSF);
cout << i+1 << " employee salary is RM" << payment << endl;
tot_payment += payment;
i++;
}
cout << endl << "Average salary is " << tot_payment / N << endl << endl;
}
void salary3(double hours[N]) {
double payment, tot_payment=0.0;
int i=0;
do {
payment = (hours[i] * PAYMENT_RATE) * (1 - KWSF);
cout << i+1 << " employee salary is RM" << payment << endl;
tot_payment += payment;
i++;
} while (i<N);
cout << endl << "Average salary is " << tot_payment / N << endl << endl;
}
int main()
{
double hours[N] = {8, 6, 8, 4, 9};
salary1(hours);
salary2(hours);
salary3(hours);
}
Comments
Leave a comment