Chapter 2 – Q #10: Miles per Gallon – Tony Gaddis – Starting Out With C++
Problem: -
A car holds 12 gallons of gasoline and can travel 350 miles before refueling. Write a program that calculates the number of miles per gallon the car gets. Display the result on the screen.
Hint: Use the following formula to calculate miles per gallon (MPG):
MPG = Miles Driven / Gallons of Gas Used
Solution: -
#include <iostream>
using namespace std;
int main()
{
int gallon = 12;
int miles = 350;
int mpg;
mpg = gallon * miles;
cout << "Number Of Miles Per Gallon is " << mpg;
return 0;
}
Explanation of this Solution
- I declared variable gallon and initialize given value.
- I declared variable miles and initialize given value.
- I declared variable mpg.
- Them, calculate value by using given formula and store mpg variable.
- In last, display output on the screen.
- Return 0 to the main function.
Also, I attach CPP file of this problem. You can download this file
0 Comments