FunctionX - Practical Learning Logo

C++ Examples: If-Else


Exercise The LeVan Car Rental company charges $0.25/mile if the total mileage does not exceed 100. If the total mileage is over 100, the company charges $0.25/mile for the first 100 miles, then it charges $0.15/mile for any additional mileage over 100.

Write a program so that if the clerk enters the number of miles, the program would display the total price owed.


Source File
#include <iostream>
using namespace std;

void main()
{
    unsigned int Miles;
    const double LessThan100 = 0.25;
    const double MoreThan100 = 0.15;
    double PriceLessThan100, PriceMoreThan100, TotalPrice;

    cout << "Enter the number of miles: ";
    cin >> Miles;

    if(Miles <= 100)
    {
        PriceLessThan100 = Miles * LessThan100;
        PriceMoreThan100 = 0;
    }
    else
    {
        PriceLessThan100 = 100 * LessThan100;
        PriceMoreThan100 = (Miles - 100) * MoreThan100;
    }

    TotalPrice = PriceLessThan100 + PriceMoreThan100;

    cout << "\nTotal Price = $" << TotalPrice << "\n\n";
}

Here is an example of running the program:
 
Enter the number of miles: 75

Total Price = $18.75

Press any key to continue

C++ Tutorial Copyright © 2001-2009 FunctionX, Inc.