Convert joule to calorie in Java

In this section, we are going to study how to convert joule to calorie in Java. Let us first see the conversion as 1 calorie = 4.184 Joule.

Here we are dividing 4.184 and we will get the calories from joules.

So if user Enters  10 J then the output will be 2.39006 cal

Java program to convert joule to calorie in Java

import java.util.*;

public class jouleConToCal {

  public static void main(String[]args) {

  Scanner in=new Scanner(System.in);
  System.out.print("Enter the number of joules(J) ");
  double joules = in.nextDouble();
  double calories = joules / 4.184;
System.out.println("The converted value of Joules to Calories is " + calories +"cal");
}
}

Output:

Enter the number of joules(J) 27 The converted value of Joules to Calories is 6.453154875717017cal

Here we are storing the values in a double variable as we will get many numbers after point and then we are using the object of scanner function to get the value of joules in a double variable by in.nextDouble.

After that, we are calculating the calories by using the formula and generating the answer stored in variable cal which is printed.

This all can be done by making a new class and calculating all the things there and then by initializing the object of we can call the class method and get the things printed there.

You may also read:

Leave a Reply

Your email address will not be published. Required fields are marked *