Convert Fahrenheit to Celsius in Java

Previously we have developed a Java program to convert the temperature from Celsius to Fahrenheit. In a similar way, we can also write Java program to convert the temperature from Fahrenheit to Celsius.

The formula that will be used for this problem is, ⁰C = ( 5 / 9 ) * ( ⁰F – 32 )

import java.util.Scanner;

public class TemperatureConversion {

  // method to convert fahrenheit to celsius
  public static double toCelsius(double fahrenheit){
     return ((5.0/9.0) * (fahrenheit - 32));
  }

  public static void main(String[] args) {

     // declare variables
     double celsius = 0.0;
     double fahrenheit = 0.0;

     // create Scanner class object
     Scanner scan = new Scanner(System.in);

     // read input
     System.out.print("Enter Fahrenheit value::");
     fahrenheit = scan.nextDouble();

     // convert temperature
     celsius = toCelsius(fahrenheit);

     // display result
     System.out.format("Celsius value = %.2f",
                          celsius);

     // close Scanner class object
     scan.close();
  }
}

The output for different test-cases:-

Enter Fahrenheit value:: 98
Celsius value = 36.67

Enter Fahrenheit value:: 50
Celsius value = 10.00

Enter Fahrenheit value:: -40
Celsius value = -40.00

Note:- The -40⁰C is equal to the -40⁰F.

Java Program to convert Fahrenheit to Celsius In a range

Generally, we need to convert Fahrenheit to Celsius in between an interval. So, now, we will develop a Java program to convert the temperature from Celsius to Fahrenheit in between a given range with a step like Fahrenheit = 0,20,40,60,80….,200. There are various methods, some of which are given here.

import java.util.Scanner;

public class TemperatureConversionInRange {
  // method to convert fahrenheit to celsius
  private static double toCelsius(double fahrenheit){
     return ((5.0/9.0) * (fahrenheit - 32));
  }

  public static void main(String[] args) {

     // declare variables
     int lower, upper, step;

     // create Scanner class object
     Scanner scan = new Scanner(System.in);

     // read input
     System.out.print("Enter Lower limit:: ");
     lower = scan.nextInt();
     System.out.print("Enter Upper limit:: ");
     upper = scan.nextInt();
     System.out.print("Enter step:: ");
     step = scan.nextInt();

     // convert temperature
     System.out.println("Fahrenheit \t"+
                         "Celsius");

     for(int i=lower; i<=upper; i+=step)
         // display result
         System.out.format("%d \t\t %.2f\n",
                     i, toCelsius(i) );

     // close Scanner class object
     scan.close();
  }
}

The output for the different test-cases:-

Enter Lower limit:: 0
Enter Upper limit:: 200
Enter step:: 20
Fahrenheit Celsius
0 -17.78
20 -6.67
40 4.44
60 15.56
80 26.67
100 37.78
120 48.89
140 60.00
160 71.11
180 82.22
200 93.33


Similar Java programming examples

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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