fork download
  1. // Renee V. Lopez CS1A Chapter 3, P. 143, #1
  2. /**************************************************************************
  3.  
  4.  * CALCULATE MILES PER GALLON
  5.  * _____________________________________________________________________________
  6.  * This program computes the distance a car can travel on one
  7.  * gallon of gas based on gas tank capacity and average
  8.  * miles-per-gallon (MPG) rating for city and highway driving.
  9.  *
  10.  * Computation is based on the formula:
  11.  * Distance = Total Miles / Number of Gallons
  12.  * _____________________________________________________________________________
  13.  * INPUT
  14.  * numOfGallons : Gas tank capacity
  15.  * totalMiles : Total miles driven
  16.  * milesPerGallon : Miles per gallon
  17.  *
  18.  * OUTPUT
  19.  * distance : Distance car can travel
  20.  *
  21.  ******************************************************************************/
  22. #include <iostream>
  23. using namespace std;
  24.  
  25. int main() {
  26. int numOfGallons; // Gas tank capacity in gallons
  27. int totalMiles; // Total miles driven on a full tank
  28. float milesPerGallon; // Miles per gallon the car can achieve
  29.  
  30. // Request user input for tank capacity and total miles driven
  31. cout << "Enter the number of gallons of gas the car can hold: ";
  32. cin >> numOfGallons;
  33.  
  34. cout << "Enter the number of miles it can be driven on a full tank: ";
  35. cin >> totalMiles;
  36.  
  37. // Calculate miles per gallon
  38. milesPerGallon = float(totalMiles) / numOfGallons;
  39.  
  40. // Display the result
  41. cout << "Miles per gallon is: " << milesPerGallon << endl;
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0.01s 5280KB
stdin
14
stdout
Enter the number of gallons of gas the car can hold: Enter the number of miles it can be driven on a full tank: Miles per gallon is: 2340.43