fork download
  1. //Jeremy Huang CS1A Chapter 3, P. 143, #1
  2. //
  3. /**************************************************************
  4.  *
  5.  * COMPUTE MPG
  6.  * ____________________________________________________________
  7.  * This program computes the miles per gallon in a car by taking
  8.  * the amount of miles it can travel on a full tank and dividing
  9.  * it by its tank capacity.
  10.  * ____________________________________________________________
  11.  * INPUT
  12.  * tankCapacity : Gas tank capacity in gallons
  13.  * miles : miles that can be driven on a full tank
  14.  *
  15.  * OUTPUT
  16.  * mpg: the miles per gallon
  17.  *
  18.  **************************************************************/
  19. #include <iostream>
  20. using namespace std;
  21.  
  22. int main() {
  23. float tankCapacity; //INPUT - Gas tank capacity in gallons
  24. float miles; //INPUT - miles that can be driven on a full tank
  25. float mpg; //OUTPUT - the miles per gallon
  26.  
  27. // Ask user for amount of gallons
  28. cout<<"Enter the number of gallons of gas the car can hold: ";
  29. cin>>tankCapacity;
  30.  
  31. // Ask user for number of miles
  32. cout<<"\nEnter the number of miles that can be driven on a full tank: ";
  33. cin>>miles;
  34.  
  35. // Compute MPG
  36. mpg = miles/tankCapacity;
  37.  
  38. // Output Result
  39. cout<<"\nThe car's mpg is: "<<mpg;
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0.01s 5320KB
stdin
40
300
stdout
Enter the number of gallons of gas the car can hold: 
Enter the number of miles that can be driven on a full tank: 
The car's mpg is: 7.5