Celsius to Fahrenheit Conversion in Java

The formula for converting temperature in Celsius to Fahrenheit is,

Fahrenheit = (9/5) * Celsius + 32

For example, if the temperature is 35 in Celsius, the equivalent value in Fahrenheit is (9/5) * 35 + 32 = 95 F.

The formula for converting temperature in Fahrenheit to Celsius is,

Celsius = (5/9) * (Fahrenheit - 32)

For example, if the temperature is 95 in Fahrenheit, the equivalent value in Celsius is (5/9)* (95-32) = 35.

The following table is a conversion table between Celsius and Fahrenheit values (some values are approximate)

Celsius (C) Fahrenheit (F)
100 212
40 104
37 98.6
30 86
21 70
10 50
0 32
-18 0
-40 -40

 

Java Program to Convert Celsius to Fahrenheit

The following Java program converts the user entered temperature value in Celsius to Fahrenheit,

// Celsius to Fahrenheit conversion in Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CelsiusToFahrenheit {
    
    public static void main(String[] args) throws IOException{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Please enter temperature in Celsius : ");
        double celsius = Double.parseDouble(reader.readLine());
        double fahrenheit = (9.0/5.0)*celsius + 32;
        System.out.println("Temperature in Fahrenheit is : "+fahrenheit);
    }
}

Note the use of double values 9.0 and 5.0. If we use 9 and 5, the results would be wrong since 9/5 would evaluate to an integer.

Java Program to Convert Fahrenheit to Celsius

The following Java program converts the user entered temperature value in Fahrenheit to Celsius ,

// Fahrenheit to Celsius conversion in Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class FahrenheitToCelsius {
    public static void main(String[] args) throws IOException{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Please enter temperature in Fahrenheit : ");
        double fahrenheit = Double.parseDouble(reader.readLine());
        double celsius = (5.0/9.0)*(fahrenheit - 32);
        System.out.println("Temperature in Celsius is : "+celsius);
    }
}