answersLogoWhite

0


Best Answer

That depends on the language. In C++ you might use the following:

#include<iostream>

using namespace std;

int main()

{

int miles, gas;

cout<<"Input the number of miles driven: ";

cin>>miles;

cout<<"Input the gallons of gas used: ";

cin>>gas;

cout<<"MPG: "<<miles/gas<<endl;

}

Of course when extracting user input you must always test the input is valid before operating upon it. If the user enters 0 gallons then you will end up with a divide by zero error.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can you create a program that asks the user for the number of miles driven and the gallons of gas used?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you compute fuel consumption?

number of miles driven on a tank / number of gallons to replenish that tank.


What is the number of gas gallons needed based on mileage and fuel efficiency?

Miles driven divided by gallons used equals miles per gallon.


Create a program which accepts a number not less than 10The program prints the reverse of the number?

ten


How do you calculate the mpg of your car?

Fill the fuel tank to the top and record the mileage. The next time you fill up record the mileage and number of gallons. Now divide the number of gallons into the number of miles driven since last fill up and you will have the mpg.


How do you caculate mileage on car?

A basic simple way of calculating mileage is taking a look at the number of miles you have driven your car and divide it with the number of gallons your car consumed to travel that long.


If you have a car with a fuel efficiency of 30 miles per gallon Define a function f that has number of miles driven as its independent variable and number of gallons used as its dependent variable?

6


How much does diesel cost to the mile?

It depends on you fuel economy. MPG means miles divided by gallons. Miles/Gallon. Take number of miles driven, divided by number of gallons to drive those miles. Flip over miles per gallon to get gallons per mile then multiply by dollars per gallon to get dollars per mile.


How do you create a program that will ask the user for a month and number of days in it?

i Have no Idea please someone tell me


How do you calculate mpg from total miles driven and gallons of gas used?

Divide the number of miles by the amount of fuel you used to go that distance. For example, if you traveled 400 miles and used 15 gallons of fuel, your fuel economy was -400 / 15 = about 26.6 mpg


How many files are created for c program when execute the program?

That's entirely up to the programmer. A program could create 0 files or 50 million files (or any number in between).


What is the formula for gallons to liters?

To convert from US Gallons to Liters, multiply the number of Gallons by 3.7854 To convert from UK Gallons to Liters, multiply the number of Gallons by 4.54609188.


How do you convert gallons per second into gallons per minute?

If there are x number of gallons per second, multiply that number by 60=gallons per minute.