Write a program that will calculate a person\'s weekly gross pay (gross pay is p
ID: 3863177 • Letter: W
Question
Write a program that will calculate a person's weekly gross pay (gross pay is pay before taxes).
A person who is paid by the hour receives weekly gross pay based on their pay rate ($/hr.) multiplied by the number of hours worked. In addition, if the person works more than 40 hours, they are paid "time-and-a-half" (1.5x) for hours in excess of 40 hours.
The program will ask the user to enter the pay rate of the person (as a double), followed by the number of hours worked (as a double), and will display the weekly gross pay.
(1) Using a loop, continue to ask for additional pay rates (and hours) until a pay rate of zero is entered. (3 pts)
Ex:
Ex:
Ex:
(2) Using loops, validate the pay rate is not negative. The validate messages are indented 3 spaces. (3 pts)
Ex:
Ex:
Ex:
(3) Using loops, validate the number of hours worked is greater than zero. The validation messages are indented 3 spaces. (2 pts)
Ex:
Ex:
and here is my code i need help please
#include <stdio.h>
#include <math.h>
int main(void) {
double hoursPay = 0.0;
double payRate = 0.0;
double pay = 0.0;
/* Type your code here. */
printf("Enter the pay rate (0 to quit): $");
while (payRate < 1) {
if (payRate <1) {
printf("Pay rate must be positive.");
}
else {
printf("Enter the number of hours worked: ");
}
}
for (hoursPay <1) {
if (hoursPay<1) {
printf("Hours worked must be positive.");
}
else {
pay = payRate * hoursPay;
printf("Pay: $%.2lf", pay);
}
}
return 0;
}
Explanation / Answer
#include <stdio.h>
#include <math.h>
int main(void)
{
double hoursPay = 0.0;
double payRate = 0.0;
double pay = 0.0;
double extra_hours = 0.0;
while(1)
{
printf("Enter the pay rate (0 to quit): $");
scanf("%lf",&payRate);
if (payRate == 0.0)
{
break;
}
else if(payRate < 0.0)
{
printf(" Pay rate must be positive. ");
}
else
{
printf("Enter the number of hours worked: ");
scanf("%lf",&hoursPay);
while (hoursPay <0)
{
fflush(stdin);
printf(" Hours worked must be positive. ");
printf("Enter the number of hours worked: ");
scanf("%lf",&hoursPay);
}
if(hoursPay > 40.0)
{
extra_hours = hoursPay - 40.0;
pay = payRate * 40.0 + payRate*1.5*extra_hours;
}
else
{
pay = payRate * hoursPay;
}
printf("Pay: $%.2lf ", pay);
}
fflush(stdin);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.