Saturday, October 23, 2021

Fuel Efficiency Through Java Code

 In this task, you are being asked to write a given word problem as a program.

Write a program that asks the user to input (using a Scanner object):

  •  The number of gallons of fuel in the tank
  •  The fuel efficiency in miles per gallon
  •  The price of fuel per gallon
Then print the cost per 100 miles and how far the car can go with the gas in the tank.

Code: 

import java.util.Scanner;
public class Lab4Task6{
public static void main(String[] args) {
Scanner Ahsan = new Scanner(System.in);
double noOfGalons, fuelEfficiency, price;
int hundredmile = 100;
System.out.print("Enter the no of Gallons of gas in tank: ");
noOfGalons = Ahsan.nextDouble();
System.out.print("Enter the Fuel Efficiency: ");
fuelEfficiency = Ahsan.nextDouble();
System.out.print("Enter the Price of Fuel per gallon: ");
price = Ahsan.nextDouble();
double CostPerHundredmile = (hundredmile / fuelEfficiency ) * price;
System.out.println("Cost of per 100 mile is: " + CostPerHundredmile);
double distance = noOfGalons * fuelEfficiency;
System.out.println("Total distance cover by car is: " + distance + "miles");
}

OutPut:

No comments:

Post a Comment

String Array and passing method in java

 In this task, we pass a method and also cover the passing String Array to the method. Code:   import java.util.Scanner; public class String...