Chapter 2 – Q #10: Miles per Gallon – Tony Gaddis – Starting Out With C++

Chapter 2 – Q #10:  Miles per Gallon – Tony Gaddis – Starting Out With C++






Miles per Gallon


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;

}

This is the solution of this question



OUTPUT OF THIS QUESTION



Explanation of this Solution


  1. I declared variable gallon and initialize given value.
  2. I declared variable miles and initialize given value.
  3. I declared variable mpg.
  4. Them, calculate value by using given formula and store mpg variable.
  5. In last, display output on the screen.
  6. Return 0 to the main function.

Also, I attach CPP file of this problem. You can download this file

Click Here to Download This File

Post a Comment

0 Comments