Ask Question

Write a C+ + program that prompt the user to enter the distance to drive, the fuel efficiency of the car in miles per gallon, and the price per gallon, and displays the cost of the trip.

+3
Answers (1)
  1. 4 August, 05:50
    0
    Here is the code in c++.

    //include headers

    #include

    using namespace std;

    int main () {

    //variables to store input

    float dis, fuel_effi, price_gall;

    cout<<" Enter total distance of the trip: ";

    //read distance

    cin>>dis;

    cout<<" fuel efficiency of car (miles per gallon) : ";

    //read fuel efficiency

    cin>>fuel_effi;

    cout<<" price of fuel (per gallon) : ";

    //read price of fuel

    cin>>price_gall;

    / / calculate total cost of trip

    float tot_cost=dis*price_gall/fuel_effi;

    cout<<"Total cost of the trip is: "<
    return 0;

    }

    Explanation:

    First read distance to travel, fuel efficiency of the car and price of fuel per gallon. To find the total cost of the trip, we use the formula total cost=dis*price_gall/fuel_effi. Where "dis" is total distance of trip, "price_gall" is price of fuel per gallon and "fuel_effi" is fuel efficiency of the car. The mentioned formula gives the total cost of the trip.

    Output:

    Enter total distance of the trip: 120

    fuel efficiency of car (miles per gallon) : 30

    price of fuel (per gallon) : 100

    Total cost of the trip is: 400
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a C+ + program that prompt the user to enter the distance to drive, the fuel efficiency of the car in miles per gallon, and the price ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers