Sample Program: If Statement


//  This program reads the hours worked during a week and the hourly pay rate
//  and calculates the gross pay, taxes owed, and net pay

#include <iostream.h>
#include <iomanip.h>

int main()
{
   float hoursWorked;
   float hourlyPay;
   float grossPay;
   float taxes;
   float takeHomePay;
   const float TAXRATE = .05;
   const float FULLTIME = 40;

//  read the input values
   cout << "Enter the number of hours worked: ";
   cin >> hoursWorked;
   cout << "Enter the hourly pay rate: ";
   cin >> hourlyPay;

//  calculate pay before taxes, including time and a half for overtime
   if (hoursWorked <= FULLTIME)
      grossPay = hourlyPay * hoursWorked;
   else
      grossPay = FULLTIME * hourlyPay +
                      (hoursWorked - FULLTIME) * hourlyPay * 1.5;

//  calculate taxes and take home pay
   taxes = grossPay * TAXRATE;
   takeHomePay = grossPay - taxes;

//  set up output so float variables will print with two decimal places
   cout.setf(ios::fixed,ios::floatfield);
   cout.setf(ios::showpoint);
   cout << setprecision(2);

//  print gross pay, taxes, and take home pay
   cout << endl;
   cout << "Gross Pay      : $" << grossPay << endl;
   cout << "Taxes          : $" << taxes << endl;
   cout << "Take Home Pay  : $" << takeHomePay << endl;

   return 0;
}

Sample Output

Enter the number of hours worked: 35
Enter the hourly pay rate: 10

Gross Pay      : $350.00
Taxes          : $17.50
Take Home Pay  : $332.50

Sample Output

Enter the number of hours worked: 50
Enter the hourly pay rate: 10

Gross Pay      : $550.00
Taxes          : $27.50
Take Home Pay  : $522.50

Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2002