Please can somebody help me with the line of codes of this question:write a C++ program which inputs litre consumption given in miles/gallon and which translates these figures into the litres/kilometre format which is more common in Europe.Use the following conversion factors: 1 mile=1.609km and 1 gallon= 3.785litres

Dani AI

Generated

The replies already cover the core idea: convert the distance and volume units, then invert to get liters per kilometre and (for readability) report liters per 100 km. and sketched the algebra and flagged the basic C++ pieces. Below is a compact, robust C++ example plus practical tips to make a safe, user-friendly converter.

#include <iostream>
#include <iomanip>

int main() {
    const double KM_PER_MILE = 1.609;       // use the value given in the question
    const double L_PER_US_GALLON = 3.785;   // use the value given in the question

    double mpg;
    std::cout << "Enter miles per gallon (mpg): ";
    if (!(std::cin >> mpg)) return 0;

    if (mpg <= 0.0) {
        std::cout << "mpg must be positive.\n";
        return 0;
    }

    double liters_per_km = L_PER_US_GALLON / (mpg * KM_PER_MILE);
    double liters_per_100km = liters_per_km * 100.0;

    std::cout << std::fixed << std::setprecision(2);
    std::cout << "Liters per km: " << liters_per_km << "\n";
    std::cout << "Liters per 100 km: " << liters_per_100km << "\n";
    return 0;
}

Notes and improvements: use double for precision, validate input to avoid divide-by-zero, and format output with std::fixed/std::setprecision. Offer a loop or accept EOF to convert multiple values. Extract the conversion into a small function if you plan to reuse it.

One important gap to check: which gallon is intended (US vs imperial)? If you need imperial gallons, change the L_PER_US_GALLON constant accordingly. Also label outputs clearly so the reader knows whether the program prints L/km or L/100km. This addresses the original question from while building on the algebraic points raised by and the presentation suggestion by .

Recommended Answers

All 3 Replies

The values you need are km per mi and gl per lt not lt per gl
1mi=1.609km
1lt=0.264172gl

lt/km = (mi/gl) * (km/mi) * (gl/lt)
lt/km = (given) * 1.609 * 0.264
lt/km = (given) * 0.425

Hope that helps.

Can get you started...

#include <iostream>

You're going to need a "main" function.
cin.
cout.
and a bit of mathematics.

Normally it's liters/100 kilometers but ignoring that detail.

If you have C in miles/gallon then you need to do 3 convertions

miles -> kilometers = times by 1.609
gallon -> liters = times by 3.785 (but note that gallons in underneath the line
distance/volume -> volume/distance = 1/x

so C miles/gallon === 1/(1.609C/3.785) = 3.785/1.609C liters/kilometer

So 30 mpg = 0.0784 liters/kilometer

Which as you can see is very small which is why liters/100 kilometer is more common

So 30 mpg = 7.8 liters/100 kilometer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.