In this tutorial, we are going to learn a python program to convert Fahrenheit into Celsius. This program will be simple because we have a Fahrenheit to Celsius conversion formula for this temperature conversion. We will use this conversion formula in our java program for this conversion.
Our Fahrenheit to Celsius conversion program behavior
- Our Java program will take a Fahrenheit temperature as an input.
- Now we will use Fahrenheit to Celsius conversion formula which is ((fahrenheit32)*5)/9.
- After applying the above formula, result will be a temperature in Celsius.
Java Program to Convert Fahrenheit into Celsius
import java.util.*;
public class Main
{
public static void main(String[] args) {
float celsius, fahrenheit;
System.out.println("Program to convert Fahrenheit to Celsius" );
Scanner sc = new Scanner(System.in);
System.out.println("Please give the Fahrenheit Temperature");
fahrenheit= sc.nextFloat();
celsius = ((fahrenheit-32)*5)/9;
System.out.printf("Celsius = %.2f",celsius);
}
}
Output
Program to convert Fahrenheit to Celsius Please give the Fahrenheit Temperature 98 Celsius = 36.67
Explanation
For the input Fahrenheit temperature 96.8.
Celsius temperature = ((96.8 -32)*5)/9 = 36.00
Program Explanation
1). Imports and Class Definition
import java.util.*;
public class Main
import java.util.*;
: This line imports all the classes from thejava.util
package, which includes theScanner
class used for reading user input.public class Main
: This defines a new public class namedMain
. In Java, code is organized in classes, andMain
is the name of our class where our program code resides.
2). Main Method
public static void main(String[] args)
This is the main method where the execution of our program begins. It’s the entry point that the Java Virtual Machine (JVM) calls when it runs the program.
3). Variable Declaration
float celsius, fahrenheit;
Here, two float variables, celsius
and fahrenheit
, are declared. Float is chosen because temperatures can have decimal values, and we need to accommodate that precision.
4). Scanner for User Input
Scanner sc = new Scanner(System.in);
A Scanner
object named sc
is created to read input from the standard input stream (typically the keyboard). This object will help us capture the temperature in Fahrenheit entered by the user.
5). Prompt User for Input
System.out.println("Please give the Fahrenheit Temperature");
fahrenheit = sc.nextFloat();
The user is prompted to input the Fahrenheit temperature. The nextFloat()
method of the Scanner
object sc
is used to read a floating point number entered by the user and store it in the variable fahrenheit
.
6). Calculate Celsius
celsius = ((fahrenheit - 32) * 5) / 9;
- The formula to convert Fahrenheit to Celsius is (𝐹𝑎ℎ𝑟𝑒𝑛ℎ𝑒𝑖𝑡−32)×5/9 . This line of code applies the formula to the value in
fahrenheit
and stores the result incelsius
.
7). Output the Result
System.out.printf("Celsius = %.2f", celsius);
Finally, the converted temperature is printed out. printf
is used here to format the output to two decimal places, which is typical for displaying temperatures.