C++ Primer Plus Chapter 3 Exercise 5

c plus plusThere really isn’t much to this one. Simple division. Here is what I have:

5. Write a program that asks how many miles you have driven and how many gallons of
gasoline you have used and then reports the miles per gallon your car has gotten. Or, if
you prefer, the program can request distance in kilometers and petrol in liters and then
report the result European style, in liters per 100 kilometers.

#include <iostream>

using namespace std;

int main()
int milesDriven;
int gallonsUsed;
int mpg;

cout << "How many miles have you driven: ";
cin >> milesDriven;
cout << "How many gallon of gas have you used: ";
cin >> gallonsUsed;

// Compute here then output
mpg = milesDriven / gallonsUsed;
cout << "You are getting " << mpg << " miles per gallon, what a clunker!" << endl;

return 0;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s