Convert Fahrenheit to Celsius in Java

0
39

This tutorial entitled “Convert Fahrenheit to Celsius in Java” will teach you on converting Fahrenheit to Celsius based on user input. At this time, we are using a float variable for temperature and use a scanner for user input. Please follow all the steps to complete this tutorial.

Convert Fahrenheit to Celsius in Java Steps

1. Create a new class and name it what you want.

How Print Hello World Text in Java, Sum Two Integers Values using Java
How Print Hello World Text in Java

2. Above your Main Class, Insert an import code to access the Scanner library.

  1. import java.util.Scanner;

3. Declare a scanner and two variables inside your main method.

  1. Scanner input = new Scanner(System.in);
  2. float temper;
  3. float result;

4. After step 3, insert the code below for user input.

  1. System.out.print("Enter Fahrenheit Temperature: ");
  2. temper = input.nextFloat();

5. After step 4, insert the code below for the algorithm and output.

  1. result = (temper - 32) * 5 / 9;
  2. System.out.println("Temperature in Celsius: " + result);

6. Run your program and the output should look like the image below.

7. Complete Source Code.

  1. import java.util.Scanner;
  2. public class FahrenheitToCelsius {
  3. public static void main(String args[]){
  4. Scanner input = new Scanner(System.in);
  5. float temper;
  6. float result;
  7. System.out.print("Enter Fahrenheit Temperature: ");
  8. temper = input.nextFloat();
  9. result = (temper - 32) * 5 / 9;
  10. System.out.println("Temperature in Celsius: " + result);
  11. }
  12. }

If you have any comment or suggestions about on How to Convert Fahrenheit to Celsius in Java, feel free to leave your comment below, use the contact page of this website or use my contact information.

Email: info@kmjtech.net  |  Cell. No.: 09468362375

Facebook Comments
(Visited 41 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here