A liter is 0.264179 gallons. Write a
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?
Chapter 3 Solutions
Absolute C++
Additional Engineering Textbook Solutions
Problem Solving with C++ (10th Edition)
Absolute Java (6th Edition)
Starting Out with C++: Early Objects (9th Edition)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Starting Out with C++: Early Objects
Starting Out with Python (4th Edition)
- Write a program that asks the user to input a grade that he or she received on an exam. The grade is an integer between 0 and 100 inclusive. The program should convert the numeric grade into the equivalent letter grade. Do the conversion by using a function Letter_Grade () that converts a numeric grade in the range 0 to 100 to the equivalent letter grade. The function should have one argument, the integer grade. The return value of the function should be A if the grade is 90 to 100; B if the grade is 80 to 89; C if the grade is 70 to 79; D if the grade is 65 to 69; and F if the grade is 64 or lower. After converting the grade, the program should display the nu¬meric grade and the equivalent letter grade.arrow_forwardWrite a function that takes a gross salary and calculates the net salary by deducting: • 7.65% for FICA • x for federal taxes • y for state taxes • z for local taxes Also, deduct $75 for parking fees and $22.50 for a gym membership. The federal tax rate must be between 10 and 37 percent. The state tax rate must be between 3.25 and 11.33 percent. The local tax rate may not be greater than 3%. If any of those values are outside the valid range, return INVALID_PERCENTAGE from your function. Federal, state, local taxes, and gym membership amounts should be passed in by value. FICA and parking should be a global constant. COMPUTER LANGUAGE IN Carrow_forwardWrite a program that accepts as input: a.)miles you have driven b.)miles per gallon, car efficiency c.)gas cost per gallon. Your program the invokes the XXX( ) function which computes and returns how many gallons of gas you have consumed to drive those miles. Then your main program continues and computes the cost of the consumed gas and displays that cost. See below. Replace the XXX ( ) function name with whatever name you want and decide about the parameters. Output each floating-point value with two digits after the decimal point, which can be achieved as follows:print(f'{your_value:.2f}')arrow_forward
- Display the first N magic numbers, where N is a positive number that the user provides as input. Here, a magic number is a number whose sum of its digits eventually leads to 1. For example, 1234 is a magic number because 1 + 2 + 3 + 4 = 10 and 1 + 0 = 1, while 1235 is not (1 + 2 + 3 + 5 = 11 and 1 + 1 = 2). Write a program that prints out the first N magic numbers, seven on each line. You are required to use the following function prototype: bool isMagic(int value); // Returns true if value is a magic number The outline of this function will be as follows: Step 1: Calculate the sum of digits of the value Step 2: Repeat Step 1 until we get a single-digit Step 3: If the resulting sum is equal to 1 then it is a magic number, otherwise not Here is the sample output:arrow_forwardGiven the following: printf ( “Enter your age in years: “ ); scanf ( “%d”, &age_in_years ); int is_voting_age = ( age_in_years >= 18 ); int is_drinking_age = ( age_in_years >= 21 ); int can_be_president = ( age_in_years >= 35 ); int is_senior_citizen = ( age_in_years >= 65 ); When the age entered is 33, what are the values of the variables above? When the age entered is 12, what are the values of the variables above? Write an IF statement in a function to print ‘Yes’ for each value above that is true.arrow_forwardWrite a function feet to meters() that changes the values of meter and cm based on the user inputs feet andinches, where meters, cm, feet, and inches are all variables. Embed this function in a loop so the user can keepinputting feet and inches and the program calls feet to meters() to give updated meters and cm.You are going to print the values of meters and cm in the main function so see how ref params work.Note that 1 inch = 2.54 cmarrow_forward
- Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name (Candidate), the number of votes received (Votes Received), and the percentage of the total votes received by the candidate (% of Total Votes). Your program should also output the winner of the election. Your program must contain at least the following functions: a function to determine the sum of votes. a function to output the winner of the election. A sample output is: Candidate Votes Received % of Total Votes Johnson 5000 25.91 Miller 4000 20.73 Duffy 6000 31.09 ... ... ... Total Votes: 19300 The Winner of the Election is Duffy The program must contain Code Pattern: cout << "Candidate\s+Votes Received\s+% of Total Votes"arrow_forwardWrite a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name (Candidate), the number of votes received (Votes Received), and the percentage of the total votes received by the candidate (% of Total Votes). Your program should also output the winner of the election. Your program must contain at least the following functions: a function to determine the sum of votes. a function to output the winner of the election. A sample output is: Candidate Votes Received % of Total Votes Johnson 5000 25.91 Miller 4000 20.73 Duffy 6000 31.09 ... ... ... Total Votes: 19300arrow_forwardWrite a function to convert temperature from Fahrenheit to Celsius named “Convert F to C” that takes one double value for Fahrenheit as argument, and returns the equivalent temperature in Celsius (type double). Create a program that exercise this function by obtaining a Fahrenheit value from the user, calling the function, and displaying the value of Celsius it returns. [Formula: Celsius = (Fahrenheit – 32) * 5/9 ]arrow_forward
- Write a function so that the main program below can be replaced by the simpler code that calls function mph_and_minutes_to_miles(). Original main program: miles_per_hour = float(input()) minutes_traveled = float(input()) hours_traveled = minutes_traveled / 60.0 miles_traveled = hours_traveled * miles_per_hour print('Miles: {:f}'.format(miles_traveled)) Sample output with inputs: 70.0 100.0Miles: 116.666667 ''' Your solution goes here ''' miles_per_hour = float(input())minutes_traveled = float(input()) print('Miles: {:f}'.format(mph_and_minutes_to_miles(miles_per_hour, minutes_traveled)))arrow_forward2. Write a program that accepts the price of a meal and the tip percent as a whole number. The program is to compute the tip amount in a separate sub-function, then compute the total amount owed, and finally print out the three amounts in a separate sub-function FORMATTED AS BELOW. Total of sale is: 9999.99 Total tip amount is: 9999.99 Total amount due is: 9999.99arrow_forwardPlease post answer in Coral One lap around a standard high-school running track is exactly 0.25 miles. Write a program that takes a number of miles as input, and outputs the number of laps. Ex: If the input is 1.5, the output is: 6.0 Ex: If the input is 2.2, the output is: 8.8 Your program should define and call a function: Function MilesToLaps(float userMiles) returns float userLapsarrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning