08.01.2023 Views

Learn to Program with C_ Learn to Program using the Popular C Programming Language ( PDFDrive )

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 4 ■ Programs with Selection Logic

else

set regular pay to 40 x rate

set overtime pay to (hours – 40) x rate x 1.5

endif

set gross pay to regular pay + overtime pay

print regular pay, overtime pay and gross pay

This algorithm is implemented as Program P4.5. All the variables are declared as double so that

fractional values can be entered for hours worked and rate of pay.

Program P4.5

#include <stdio.h>

int main() {

double hours, rate, regPay, ovtPay, grossPay;

printf("Hours worked? ");

scanf("%lf", &hours);

printf("Rate of pay? ");

scanf("%lf", &rate);

if (hours <= 40) {

regPay = hours * rate;

ovtPay = 0;

}

else {

regPay = 40 * rate;

ovtPay = (hours - 40) * rate * 1.5;

}

grossPay = regPay + ovtPay;

printf("\nRegular pay: $%3.2f\n", regPay);

printf("Overtime pay: $%3.2f\n", ovtPay);

printf("gross pay: $%3.2f\n", grossPay);

}

A sample run of this program is shown here:

Hours worked? 50

Rate of pay? 12

Regular pay: $480.00

Overtime pay: $180.00

Gross pay: $660.00

79

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!