Absolute C++
Absolute C++
6th Edition
ISBN: 9780133970784
Author: Walter Savitch, Kenrick Mock
Publisher: Addison-Wesley
bartleby

Videos

Textbook Question
Book Icon
Chapter 3, Problem 1PP

A liter is 0.264179 gallons. Write a program that will read in the number of liters of gasoline consumed by the user’s car and the number of miles traveled by the car and will then output the number of miles per gallon the car delivered. Your program should allow the user to repeat this calculation as often as the user wishes. Define a function to compute the number of miles per gallon. Your program should use a globally defined constant for the number of liters per gallon.

Expert Solution & Answer
Check Mark
Program Plan Intro

List of variables:

number_of_miles: Store the number of miles travelled.

liter_of_gas: Store the quantity of gasoline consumed.

List of functions used:

cout.setf(ios::showpoint): For controlling the floating and trailing zeros.

cout.precision(2): Dislay the values up to two digits.

cin(): To take input from input streams like keyboard, files etc.

cout(): To display the output.

Summary Introduction:

Program will use the Main () method to prompt the user to enter the number of miles traveled and the number of gasoline consumed to find the number of miles per gallon car delivered. A liter is equal to 0.264179 gallons.

Program Description:

The purpose of the program is to find the number of miles per gallon car delivered.

Explanation of Solution

Program:

Following is the C++ program to find the number of miles per gallon car delivered.

/*

* Program: The program asks user to provide the number of miles car travelled and the amount of gasoline consumed to find the number of miles per gallon car delivered.

*/

#include <iostream>
Usingnamespacestd;
constdouble LITRE =0.264179;
// A liter is equal to 0.264179 gallons
doublemiles_per_gallons(float,float);
intmain(){
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
charans;
do{
// declare the variables.
Floatnumber_of_miles,liter_of_gas;
cout<<"Enter the number of miles traveled: ";
cin>>number_of_miles;
cout<<"\nEnter the number of liters of gasoline consumed: ";
cin>>liter_of_gas;
cout<<endl<<miles_per_gallons(number_of_miles,liter_of_gas);
cout<<endl<<"Do you want to calculate again? ";
cin>>ans;
}while(ans=='Y'||ans=='y');
cout<<"\nThank You!";
return0;
}
// function to calculate the miles per gallon car delivered.
doublemiles_per_gallons(floatnumber_of_miles,floatliter_of_gasoline){
returnnumber_of_miles/(liter_of_gasoline*LITRE);
}

Explanation:

The above program prompts the user to enter the number of miles traveled and the number of gasoline consumed and after that function miles_per_gallons() is defined to find the number of miles traveled by gasoline consumed in liter. Then cout() function is used to show the output of the program.

Sample Output:

Following is the sample output for the given program:

Enter the number of coupons: 25 
The number of Candy bars: 2 
The number of Gum Balls: 1 
The remaining coupons are: 2

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
A liter is 0.264179 gallons. Write a program that will read in the number of liters of gasoline consumed by the user’s car and the number of miles traveled by the car and will then output the number of miles per gallon the car delivered. Your program should allow the user to repeat this calculation as often as the user wishes. Define a function to compute the number of miles per gallon. Your program should use a globally defined constant for the number of liters per gallon.
// // Write a function that calculates the amount of money a person would earn over // a period of years if his or her salary is one penny the first day, two pennies // the second day, and continues to double each day. The program should ask the // user for the number of years and call the function which will return the total // money earned in dollars and cents, not pennies. Assume there are 365 days // in a year. // function totalEarned (years) { // Insert your code between here and the next comment block. Do not alter // // any code in any other part of this file. // Insert your code between here and the previous comment block. // any code in any other part of this file. Do not alter // // var years = parseInt(prompt('How many years will you work for pennies a day? ')); var totalDollarsEarned = totalEarned (years); alert('Over a total of ' + years + ', you will have earned $' + totalDollarsEarned);
in c++ please A liter is 0.264179 gallons. Write a program that will read in the number of liters of gasoline consumed by the user’s car and the number of miles traveled by the car and will then output the number of miles per gallon the car delivered. Your program should allow the user to repeat this calculation as often as the user wishes. Define a function to compute the number of miles per gallon. Your program should use a globally defined constant for the number of liters per gallon.

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Computer Programming for Beginners | Functions, Parameters & Arguments | Ep24; Author: Programming With Avelx;https://www.youtube.com/watch?v=VXlh-qJpfw0;License: Standard YouTube License, CC-BY