Program for Fuel Consumption

Fuel Consumption

Here, in this page we will discuss the program for fuel consumption in different languages. We will discuss the algorithm in brief for finding the solution of the problem.

Fuel Consumption

Objective :

Write a Program to Calculate the fuel consumption of your vehicle. The program should ask the user to Enter the quantity of petrol to fill up the tank and the distance covered till the tank goes dry.
Calculate the fuel consumption and display it in the format (liters per 100 kilometers). Convert the same result to the u.s. style of miles per gallon and display the result. If the quantity or distance is zero or negative display ” is an invalid input”.

[Note:  The US approach of fuel consumption calculation (distance / fuel) is the inverse of the European approach (fuel / distance).
Also note that 1 kilometer is 0.6214 miles, and 1 liter is 0.2642 gallons] .
The result should be with two decimal place. to get two decimal place refer the below-mentioned print statement : float cost=670.23
system.out.printf(“you need a sum of rs.%.2f to cover the trip”,cost)

 

Algorithm :

  • Take the liter of petrol.
  • Covert the given integer input into double. And check whether it is valid or not.
  • Similarly take the distance, convert it in double then check if it is valid or not,
  • Now, print the value obtained by (lt/dis)*100 upto 2 floating values.
  • Find miles by dis*0.6214, and gallons = lt*0.2642
  • Find mg = miles/gallons
  • And Print it upto 2 floating values.

Input :

Enter the no of liters to fill the tank

20

Enter the distance covered

150

Output :

Liters/100KM

13.33

Miles/gallons

17.6

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription