Java program to convert from Fahrenheit to Celsius with examples
Introduction
This tutorial first goes through the formulae for conversion of temperature from Fahrenheit to Celsius and vice versa. It then provides Java programs to convert from Fahrenheit to Celsius and back, along with explanation of code.
Formulae for conversion between Fahrenheit and Celsius
=> Temperature in Celsius = (Temperature in Fahrenheit - 32) * 5/9
=> Temperature in Fahrenheit = (Temperature in Celsius * 9/5) + 32 Java program to convert Fahrenheit to Celsius
OUTPUT of the above code
Explanation of the code
Java program to convert Celsius to Fahrenheit
OUTPUT of the above code
Explanation of the code
=> Temperature in Fahrenheit = (Temperature in Celsius * 9/5) + 32 Java program to convert Fahrenheit to Celsius
Java program to convert Fahrenheit to Celsius
package com.javabrahman.generaljava;
import java.util.Scanner;
public class FahrenheitToCelsius {
public static void main(String args[]){
System.out.print("Enter the temperature in Fahrenheit: ");
Scanner scanner=new Scanner(System.in);
float fahrenheit=scanner.nextFloat();
float celsius=(fahrenheit-32)*5/9;
System.out.println("Temperature in Celsius: "+celsius);
}
}
Enter the temperature in Fahrenheit: 100 Temperature in Celsius: 37.77778
- In the
main()
method ofFahrenheitToCelsius
class, first the temperature in Fahrenheit is taken as input as a primitivefloat
value using ajava.util.Scanner
instance from the console. - The value in Fahrenheit is then converted to corresponding value in Celsius using the conversion formula and the converted value is printed as output.
- As an example, temperature in Fahrenheit is input as
100
degrees and the corresponding temperature in Celsius is printed as37.77778
degrees.
Java program to convert Celsius to Fahrenheit
package com.javabrahman.generaljava;
import java.util.Scanner;
public class CelsiusToFahrenheit {
public static void main(String args[]){
System.out.print("Enter the temperature in Celsius: ");
Scanner scanner=new Scanner(System.in);
float celsius=scanner.nextFloat();
float fahrenheit=(celsius*9/5)+32;
System.out.println("Temperature in Fahrenheit: "+fahrenheit);
}
}
Enter the temperature in Celsius: 38 Temperature in Fahrenheit: 100.4
- In the
main()
method ofCelsiusToFahrenheit
class, first the temperature in Celsius is taken as input as a primitivefloat
value using ajava.util.Scanner
instance from the console. - The value in Celsius is then converted to corresponding value in Fahrenheit using the conversion formula and the converted value is printed as output.
- As an example, temperature in Celsius is input as
38
degrees and the corresponding temperature in Fahrenheit is printed as100.4
degrees.