Programming Challenge Chapter 3 – Q #1 Miles per Gallon – Tony Gaddis – Starting Out With C++
Problem: -
Write a program that calculates a car s gas mileage. The program should ask the user
to enter the number of gallons of gas the car can hold, and the number of miles it can
be driven on a full tank. It should then display the number of miles that may be driven
per gallon of gas.
Solution: -
#include <iostream>
using namespace std;
int main()
{
int gass_gallon, number_of_miles;
int result = 0;
cout << "Enter Gallon Of Gas:
";
cin >> gass_gallon;
cout << "Enter Miles: ";
cin >> number_of_miles;
result = gass_gallon * number_of_miles;
cout << "Number Of Miles Can Travel: " << result;
return 0;
}
This is the solution of this question
OUTPUT OF THIS QUESTION
Input is highlighted with yellow color.
Explanation of this Solution
- Declare two int variables for calculation.
- Declare another variable int variable.
- Take input Gallon Of Gas from the user by using cin.
- Take input Miles from the user by using cin.
- Calculate the result by multiplying gas gallon and miles,
- 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