fork download
  1. //Charlotte Davies-Kiernan CS1A Chapter 2, P. 83, #11
  2. //
  3. /****************************************************************************
  4.  *
  5.  * Compute Distance Per Tank of Gas
  6.  * __________________________________________________________________________
  7.  * This program computes the distance a car can travel on one tank of gas
  8.  * based on gas tank capacity and average miles-per-gallon (MPG) rating for
  9.  * city and highway driving
  10.  *
  11.  * Computation is based on the formula:
  12.  * Distance = Number of Gallons x Average Miles per Gallon
  13.  * __________________________________________________________________________
  14.  * INPUT
  15.  * tankCapacity : Gas tank capacity in gallons
  16.  * mpgCity : Average MPG for city driving
  17.  * mpgHighway : Average MPG for highway driving
  18.  *
  19.  * OUTPUT
  20.  * distance : Distance car can travel
  21.  *
  22.  ***************************************************************************/
  23. #include <iostream>
  24. #include <iomanip>
  25. using namespace std;
  26. int main ()
  27. {
  28. float tankCapacity; //INPUT - Gas tank capacity in gallons
  29. float mpgCity; //INPUT - Average MPG for city driving
  30. float mpgHighway; //INPUT - Average MPG for highway driving
  31. float distance; //OUTPUT - Distance car can travel
  32. //
  33. // Initialize Program Variables
  34. tankCapacity = 20.0;
  35. mpgCity = 21.5;
  36. mpgHighway = 26.8;
  37. //
  38. // Compute Distance Traveled City
  39. distance = tankCapacity * mpgCity;
  40. //
  41. // Output Result
  42. cout << "The car can travel " << distance << " miles in town." << endl;
  43. //
  44. // Compute Distance Traveled Highway
  45. distance = tankCapacity * mpgHighway;
  46. //
  47. // Output Result
  48. cout << "the car can travel " << distance << " miles highway." << endl;
  49. return 0;
  50. }
Success #stdin #stdout 0.01s 5268KB
stdin
Standard input is empty
stdout
The car can travel 430 miles in town.
the car can travel 536 miles highway.