EBK ABSOLUTE C++
EBK ABSOLUTE C++
6th Edition
ISBN: 9780133970944
Author: SAVITCH
Publisher: PEARSON CUSTOM PUB.(CONSIGNMENT)
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
Write a program to gauge the rate of an employee’s raise from the previous year. The program asks for their annual salary in this year and the previous year. It estimates the hike percentage as the difference in their salary from the previous year divided by the previous year’s salary. Your program should allow the user to repeat this calculation as often as the user wishes. Define a function to compute the hike percentage for an employee. The hike rate should be a value of type double giving the rate as a percentage, for example 5.3 for 5.3%.
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 asks the user how many Fibonacci numbers to generate and then generates them. Make sure to ask the user to enter the number of numbers in the sequence to generate. The Fibonacci sequence is a sequence of numbers where the next number in the sequence is the sum of the previous two numbers in the sequence. The sequence looks like this: 1, 1, 2, 3, 5, 8, 13, § You could assign your input integer to a variable num by num = int(input("How many fib. numbers you want to generate?:")). § You have to consider exceptions, such as the input integer is zero, negative numbers or floats. § Please submit your code and console screenshots to Blackboard. Code containing syntax error will be graded zero. § Example: The prompt asks, “How many fib. numbers you want to generate?

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
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
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