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.
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.
#include <stdio.h> // header files #include <stdlib.h> // library files int main() { printf("Enter the no of liters to fill the tank\n"); int ltt ; scanf("%d",<t); double lt= (ltt*1.00); if(ltt<1) { printf("%d is an Invalid Input\n",ltt); exit(0); } printf("Enter the distance covered\n"); int diss ; scanf("%d",&diss); double dis= (diss*1.00); if(diss<1) { printf(" %d is an Invalid Input\n",diss); exit(0); } double hundred = ((lt/dis)*100); printf("Liters/100KM\n"); printf("%.2f\n",hundred); double miles = (dis*0.6214); double gallons =(lt*0.2642); double mg = miles/gallons; printf("Miles/gallons\n"); printf("%.2f\n",mg); return 0; }
#include<bits/stdc++.h> using namespace std; int main() { cout<<"Enter the no of liters to fill the tank\n"; int ltt ; cin>>ltt; double lt= (ltt*1.00); if(ltt<1) { cout<<ltt<<" is an Invalid Input\n"; exit(0); } cout<<"Enter the distance covered\n"; int diss ; cin>>diss; double dis= (diss*1.00); if(diss<1) { cout<<diss<<" is an Invalid Input\n"; exit(0); } double hundred = ((lt/dis)*100); cout<<"Liters/100KM\n"; printf("%.2f\n",hundred); double miles = (dis*0.6214); double gallons =(lt*0.2642); double mg = miles/gallons; cout<<"Miles/gallons\n"; printf("%.2f\n",mg); return 0; }
import java.util.*; import java.text.*; class Main{ public static void main (String[] args) { DecimalFormat df2 =new DecimalFormat("0.00"); Scanner sc= new Scanner (System.in); System.out.println("Enter the no of liters to fill the tank"); int ltt =sc.nextInt(); double lt= (ltt*1.00); if(ltt<1){ System.out.println(ltt+" is an Invalid Input"); System.exit(0); } System.out.println("Enter the distance covered"); int diss =sc.nextInt(); double dis= (diss*1.00); if(diss<1){ System.out.println(diss+" is an Invalid Input"); System.exit(0); } double hundered = ((lt/dis)*100); System.out.println("Liters/100KM"); System.out.println(df2.format(hundered)); double miles = (dis*0.6214); double gallons =(lt*0.2642); double mg = miles/gallons; System.out.println("Miles/gallons"); System.out.println(df2.format(mg)); } }
import sys print("Enter the no of liters to fill the tank") ltt =int(input()) lt= (ltt*1.00) if(ltt<1): print("{} is an Invalid Input".format(ltt)) sys.exit() print("Enter the distance covered") diss =int(input()) dis= (diss*1.00) if(diss<1): print("{} is an Invalid Input".format(diss)) sys.exit() hundred = ((lt/dis)*100) print("Liters/100KM") print(round(hundred,2)) miles = (dis*0.6214); gallons =(lt*0.2642); mg = miles/gallons; print("Miles/gallons") print(round(mg,2))
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
Login/Signup to comment