6.4k views
0 votes
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.

User Deejers
by
5.7k points

1 Answer

2 votes

Answer:

Here is the code in c++.

//include headers

#include <bits/stdc++.h>

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: "<<tot_cost<<endl;

return 0;

}

Step-by-step 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

User Kartik Prasad
by
5.3k points