A floating point calculation
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
/*
* This program takes a volume in gallons and converts the quantity to several
* other volume units, finally liters.
*/
int main()
{
// Ask for the quantity.
cout << "Please enter a volume in gallons: ";
double gal;
cin >> gal;
// A gallon is defined as 231 cubic inches. So let's convert.
int cuin_per_gal = 231;
double cuin = cuin_per_gal*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.