fork download
  1. /**************************************************************
  2.   *
  3.   * MILES PER GALLON CALCULATOR
  4.   * ________________________________________________________
  5.   * This program calculates the car's gas mileage by asking the
  6.   * user to input the number of gallons of gas the car can hold
  7.   * and the number of miles it can travel on a full tank.
  8.   * The program then calculates and displays the number of miles
  9.   * the car can drive per gallon of gas.
  10.   * ________________________________________________________
  11.   * INPUT
  12.   * The program will prompt the user for:
  13.   * - The number of gallons of gas the car can hold.
  14.   * - The number of miles the car can travel on a full tank.
  15.   *
  16.   * OUTPUT
  17.   * The program will display the number of miles the car can
  18.   * drive per gallon of gas.
  19.   *
  20.   **************************************************************/
  21.  
  22. #include <iostream> // file for cout and cin
  23. using namespace std;
  24.  
  25. int main() {
  26. // Declare variables
  27. double gallons, miles, milesPerGallon;
  28.  
  29. // Ask the user for the number of gallons the car can hold
  30. cout << "Enter the number of gallons the car can hold: ";
  31. cin >> gallons;
  32.  
  33. // Ask the user for the number of miles the car can drive on a full tank
  34. cout << "Enter the number of miles the car can drive on a full tank: ";
  35. cin >> miles;
  36.  
  37. // Calculate miles per gallon
  38. if (gallons != 0) {
  39. milesPerGallon = miles / gallons;
  40.  
  41. // Display the result
  42. cout << "The car can drive " << milesPerGallon << " miles per gallon of gas." << endl;
  43. } else {
  44. cout << "Gallons cannot be zero." << endl;
  45. }
  46.  
  47. return 0;
  48. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Enter the number of gallons the car can hold: Enter the number of miles the car can drive on a full tank: The car can drive 1.48881 miles per gallon of gas.