A liter is 0.264179 gallons. Write a
- Include required header files.
- Declare and initialize a constant value “lpg = 0.264179”.
- Define a function named “calc()” to calculate “milage”.
- Declare a variable “gal”.
- Compute “gal” and “milage”
- Function to return “milage”.
- Define a “main()” function.
- Declare the variables “lit” and “miles”.
- Declare a variable “ch”.
- “do… while” loop to get the user input repeatedly.
- Get the “lit” and “miles” from the user.
- Call “calc()” with an arguments “lit” and “miles” values and print the “milage”.
- Get the user input to repeat the program or not.
- The input is checked with the condition and repeat or exit the program.
Explanation of Solution
Program to compute number of miles per gallon the car delivered:
// Include required header files
#include <iostream>
using namespace std;
// Assign const value
float const lpg=0.264179;
// Function definition of calc()
float calc(float liters, float miles)
{
// Declare gal
float gal;
// Compute gal
gal = lpg * liters;
// Compute milage
float milage = miles/gal;
// Return milage
return(milage);
}
// Function definition of main()
int main()
{
// Declare lit and miles
float lit, miles;
// Declare ch
char ch;
// do... while loop
do{
// Get the liters
cout<<"\nEnter the number of liters of gasoline: ";
// Assign the user input to lit
cin>>lit;
// Get the miles
cout<<"\nEnter the number of miles Travelled: ";
// Assign the user input to miles
cin>>miles;
// Display the miles per gallon the car delivered
cout<<"\nNumber of miles per gallon the car delivered: ";
// Call the function cal() and return the result
cout<< calc(lit, miles) << endl;
// Get the user input
cout<<"\nDo you want to repeat(y/n)??: ";
// Assign the user input to ch
cin>>ch;
// While loop condition to check ch is equal to y
}while(ch=='y' || ch=='Y');
// Return 0
return 0;
}
Enter the number of liters of gasoline: 5
Enter the number of miles Travelled: 30
Number of miles per gallon the car delivered: 22.7119
Do you want to repeat(y/n)??: n
Want to see more full solutions like this?
Chapter 4 Solutions
Problem Solving with C++ (9th Edition)
Additional Engineering Textbook Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
Computer Systems: A Programmer's Perspective (3rd Edition)
Database Concepts (8th Edition)
Starting Out with Python (4th Edition)
Starting Out with C++: Early Objects (9th Edition)
Java How To Program (Early Objects)
- In physics, an object that is in motion is said to have kinetic energy. The following formula can be used to determine a moving object’s kinetic energy: KE = 1⁄2 mv2 The variables in the formula are as follows: KE is the kinetic energy in joules, m is the object’s mass in kilograms, and v is the object’s velocity in meters per second.Write a function named kineticEnergy that accepts an object’s mass (in kilograms) and velocity (in meters per second) as arguments. The function should return the amount of kinetic energy that the object has. Demonstrate the function by calling it in a program that asks the user to enter values for mass and velocity.arrow_forwardWrite a program that takes input two numbers for and store it in two variables named gross_salary and savings. The program then passes these two values to a function named calculate_tax(). The calculate_tax() function determines the tax amount as per the following criteria: i. If savings are less than 100,000, the whole amount will not be included in taxable income. However, if the amount is more than 100,000 (suppose 110,000), the amount that will be included in the taxable income will be calculated as follows: Savings - 100,000 as we supposed our savings are 110,000, the above formula will give value as under: 110,000 - 100,000 = 10,000 This means, if we have savings of 110,000, only 10,000 will be included in taxable income. So, the total taxable income will be calculated as under if (savings >100.000) taxable income = gross_salary + (savings - 100,000) else taxable_income = gross_salary + savings Note : this c++ program please try to solve within 30 minutesarrow_forwardIn physics, an object that is in motion is said to have kinetic energy. The following formula can be used to determine a moving object's kinetic energy: KE = ½ m v2 The variables in the formula are as follows: KE is the kinetic energy, m is the object's mass in kilograms, and v is the object's velocity, in meters per second. Write a function named kineticEnergy that accepts an object's mass (in kilograms) and velocity (in meters per second) as arguments. The function should return the amount of kinetic energy that the object has. Demonstrate the function by calling it in a program that asks the user to enter values for mass and velocity.arrow_forward
- Write a function to determine the cost of an automobile insurance premium, based on driver's age and the number of accidents that the driver has had. The basic insurance charge is $500. There is a surcharge of $100 if the driver is under 25 and an additional surcharge for accidents: \# of accidents Accident Surcharge 1 50 2 125 3 225 4 375 5 575 6 or more 700 Then call the function to print the result. example (underline denotes input values): ```Age? 26``` ```Accidents? 3``` ```$725```arrow_forwardIn this question you need to write a program and execute it with the given input, the output of your code is the answer to this question and in the next question paste your code. Write a function GregorianConvíyear) that takes a Islamic(Lunar) year, then returns the corresponding Gregorian year. You have to TEST that the input is a valid number in the range of 1300 to the current year 1442.Year Gregorian = (Year Muslim ) / 0.97 + 622, Note: as the two calendars don't start from the same month, year 2021 corresponds to the years 1442-1443 in the Islamic calendar. So round your solution. What will be the Gregorian year for the Islamic year 1320? Answer:arrow_forwardpythonarrow_forward
- One lap around a standard high-school running track is exactly 0.25 miles. Define a function named laps_to_miles that takes a number of laps as a parameter, and returns the number of miles. Then, write a main program that takes a number of laps as an input, calls function laps_to_miles() to calculate the number of miles, and outputs the number of miles. Output each floating-point value with two digits after the decimal point, which can be achieved as follows: print (f' {your_value:.2f}') Ex: If the input is: 7.6 the output is: 1.90 Ex: If the input is: 2.2 the output is: 0.55 The program must define and call the following function: def laps_to_miles (user_laps) 461710.3116374.qx3zqy7 LAB ACTIVITY 1 # Define your function here 21 3 00 Jo UAWN P 4 if ___name__ 5 6 7.8.1: LAB: Track laps to miles 7 8 main.py '__main__': # Type your code here. Your code must call the function. 8/10 Load default template...arrow_forwardOne lap around a standard high-school running track is exactly 0.25 miles. Define a function named laps_to_miles that takes a number of laps as a parameter, and returns the number of miles. Then, write a main program that takes a number of laps as an input, calls function laps_to_miles() to calculate the number of miles, and outputs the number of miles. Output each floating-point value with two digits after the decimal point, which can be achieved as follows: print (f' {your_value:.2f}') Ex: If the input is: 7.6 the output is: 1.90 Ex: If the input is: 2.2 the output is: 0.55 The program must define and call the following function: def laps_to_miles (user_laps) 461710.3116374.qx3zqy7 LAB ACTIVITY 1 # Define your function here 2 456WNI 3 4 7.8.1: LAB: Track laps to miles 6 main.py 5 # Type your code here. Your code must call the function. 8/10 Load default template...arrow_forwardOne lap around a standard high-school running track is exactly 0.25 miles. Define a function named laps_to_miles that takes a number of laps as a parameter, and returns the number of miles. Then, write a main program that takes a number of laps as an input, calls function laps_to_miles() to calculate the number of miles, and outputs the number of miles. Output each floating-point value with two digits after the decimal point, which can be achieved as follows: print (f'{your_value:.2f}') Ex: If the input is: 7.6 the output is: 1.90 Ex: If the input is: 2.2 the output is: 0.55 The program must define and call the following function: def laps_to_miles (user_laps) 461710.3116374.qx3zqy7 LAB ACTIVITY 7.8.1: LAB: Track laps to miles 1 # Define your function here 2 3 if __name__ 4 main.py == '__main__': #Type your code here. Your code must call the function. 0/10 Load default template...arrow_forward
- A "jiffy" is the scientific name for 1/100th of a second. Define a function named jiffies_to_seconds that takes the number of "jiffies" as a parameter, and returns the number of seconds. Then, write a main program that reads the number of jiffies (float) as an input, calls function jiffies_to_seconds() with the input as argument, and outputs the number of seconds. Output each floating-point value with three digits after the decimal point, which can be achieved as follows:print(f'{your_value:.3f}') Ex: If the input is: 15.25 the output is: 0.152 The program must define and call the following function: def jiffies_to_seconds(user_jiffies) MY CODE: def jiffies_to_seconds(user_seconds): jiffies = user_seconds*100 return jiffies #main of programif __name__ == '__main__': user_seconds = float(input()) jiffies = jiffies_to_seconds (user_seconds) print(f'{user_seconds:.3f}') ERROR: Not understanding the correct function here. The output is putting the decimal in the wrong…arrow_forwardThe following program will prompt the user for a starting number and an // ending number. Assume that these are whole numbers (no decimal points), // assume the numbers are not negative and assume that the first number is // not larger than the second number. // // write a funcTion that returns the sum of the numbers from the starting // number to the ending number. For example, if the starting number is 13 // and the ending number is 18 then the function will return the value of // 13 + 14 + 15 + 16 + 17 + 18. If the starting number is equal to the ending // number then simply return that number. // ////////////////////////////////////////////////////////////////////////// function sumOfNumbers(startingNumber, endingNumber) { } var startingNumber = parseInt(prompt('What is the starting number? ')); var endingNumber = parseInt(prompt('What is the ending number? ')); var sumOfNumbersPassed = sumOfNumbers(startingNumber, endingNumber); alert('The sum of the numbers from ' +…arrow_forwardWrite a function that computes the average of the digits in an integer. Use the following function header: double averageDigits(long n)arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr