Answer to Question #158448 in C++ for Rishit

Question #158448

Driving is expensive. Write a program with a car's miles/gallon and gas dollars/gallon (both doubles) as input, and output the gas cost for 20 miles, 75 miles, and 500 miles.

Output each floating-point value with two digits after the decimal point, which can be achieved by executing

cout << fixed << setprecision(2); once before all other cout statements.


1
Expert's answer
2021-01-25T14:58:48-0500
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
    // distance in miles
    float distance1 = 20.0, distance2 = 75.0, distance3 = 500.0;
    float miles_gallon, dollars_gallon;
    cout << "Enter cars miles/gallon: "; cin >> miles_gallon;
    cout << "Enter cars dollars/gallon: "; cin >> dollars_gallon;
    cout << "the gas cost for " << distance1 << " miles is " << fixed << setprecision(2) << (float) dollars_gallon * distance1 / miles_gallon << "$\n";
    cout << "the gas cost for " << distance2 << " miles is " << fixed << setprecision(2) << (float) dollars_gallon * distance2 / miles_gallon << "$\n";
    cout << "the gas cost for " << distance3 << " miles is " << fixed << setprecision(2) << (float) dollars_gallon * distance3 / miles_gallon << "$\n";
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog