fork download
  1. //Cameron Pham CS1A Chapter 3, P. 143, #1
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * Calculate Miles Per Gallon
  6.  * _____________________________________________________________________________
  7.  *
  8.  * This program calculates a car's gas mileage in miles per gallon (MPG).
  9.  *
  10.  * Computation is based on the formula:
  11.  * Miles per Gallon = Miles Driven / Gallons of Gas
  12.  * _____________________________________________________________________________
  13.  *
  14.  * INPUT
  15.  * gallons : Number of gallons of gas the car can hold
  16.  * miles : Miles the car can travel on a full tank
  17.  *
  18.  * OUTPUT
  19.  * milesPerGallon : The car's miles per gallon (MPG)
  20.  *
  21.  ******************************************************************************/
  22. #include <iostream>
  23. using namespace std;
  24.  
  25. int main()
  26. {
  27. // Declare variables to store the user's input and the MPG calculation.
  28. double gallons;
  29. double miles;
  30. double mpg;
  31.  
  32. // Ask the user how many gallons of gas the car can hold.
  33. cout << "Enter the number of gallons the car can hold: ";
  34. cin >> gallons;
  35.  
  36. // Ask the user how many miles the car can travel on a full tank.
  37. cout << "Enter the number of miles the car can be driven on a full tank: ";
  38. cin >> miles;
  39.  
  40. // Calculate miles per gallon by dividing the total miles by the gallons.
  41. mpg = miles / gallons;
  42.  
  43. // Display the calculated miles per gallon.
  44. cout << "The car gets " << mpg << " miles per gallon." << endl;
  45.  
  46. return 0;
  47. }
Success #stdin #stdout 0s 5304KB
stdin
15
450
stdout
Enter the number of gallons the car can hold: Enter the number of miles the car can be driven on a full tank: The car gets 30 miles per gallon.