A floating point calculation
using namespace std;
* This program takes a volume in gallons and converts the quantity to several
* other volume units, finally liters.
// Ask for the quantity.
cout << "Please enter a volume in gallons: ";
cin >> gal;
// A gallon is defined as 231 cubic inches. So let's convert.
double cuin = 231*gal;
cout << gal << " = " << cuin << " cubic inches." << endl;
// An inch is defined as 2.54 centimeters. So on we go.
double cm_per_in = 2.54; // Centemeters per inch.
double cc = cuin * cm_per_in * cm_per_in * cm_per_in;
cout << " = " << cc << " cubic centimeters." << endl;
// A cc is the same as a ml, so we can get liters.
cout << " = " << cc / 1000.0 << " liters." << endl;
This program does some simple calculation. The declaration
of variables as double
, and assignment statements using them
are familiar from Java. Likewise the use of floating-point constants.