fork download
  1. // Torrez, Elaine CS1A Chapter 2 P. 81, #1
  2.  
  3. /******************************************************************************************
  4.  *
  5.  * CALCULATE MILES PER GALLON
  6.  *
  7.  * --------------------------------------------------------------------------------
  8.  * This program calculates a car’s gas mileage. It uses the number of gallons of
  9.  * gas the car can hold and the number of miles it can be driven on a full tank.
  10.  * The program then calculates and displays the number of miles per gallon.
  11.  * --------------------------------------------------------------------------------
  12.  *
  13.  * INPUT
  14.  * gallons : Number of gallons of gas the car can hold
  15.  * miles : Number of miles the car can be driven on a full tank
  16.  *
  17.  * OUTPUT
  18.  * mpg : Miles per gallon of gas
  19.  *
  20.  *******************************************************************************************/
  21.  
  22. #include <iostream>
  23. #include <iomanip> // Included for fixed and setprecision
  24. using namespace std;
  25.  
  26. int main ()
  27. {
  28. double gallons; // INPUT number of gallons the tank holds
  29. double miles; // INPUT number of miles the car can drive on a full tank
  30. double mpg; // OUTPUT miles per gallon
  31.  
  32. // Hardcoded test values (change if needed)
  33. gallons = 12;
  34. miles = 144;
  35.  
  36. // Calculate miles per gallon
  37. mpg = miles / gallons;
  38.  
  39. // Format and display result
  40. cout << fixed << setprecision(1);
  41. cout << miles << " miles / " << gallons << " gallons = "
  42. << mpg << " miles per gallon." << endl;
  43.  
  44. return 0;
  45. }
  46.  
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
144.0 miles / 12.0 gallons = 12.0 miles per gallon.