How to convert fahrenheit to celsius in Java

3 Answers

0 votes
import java.util.Scanner;
 
public class MyClass 
{
    public static void main(String[] args) 
    {
        try
        {
            Scanner in = new Scanner(System.in); 
            System.out.print("Enter Temperature in Fahrenheit: ");
            int temp_f = in.nextInt();
            double temp_c = FahrenheitToCelsius(temp_f);
            System.out.format("%d Fahrenheit = %.2f Celsius\n", temp_f, temp_c); 
        }
        catch (Exception e)
        {
           System.out.println(e.toString());
        }  
    }
    static double FahrenheitToCelsius(int f)
    {
        double t;
  
        t = (f - 32.0) / 1.8;
  
        return t;
    }
}


 
/*
 
run 1:
 
Enter Temperature in Fahrenheit: 1
1 Fahrenheit = -17.22 Celsius
 
run 2:
  
Enter Temperature in Fahrenheit: 50
50 Fahrenheit = 10.00 Celsius
 
*/


answered May 6, 2015 by avibootz
edited Jul 20, 2022 by avibootz
0 votes
import java.util.Scanner;
 
public class MyClass 
{
    public static void main(String[] args) 
    {
        try
        {
            Scanner in = new Scanner(System.in); 
            System.out.print("Enter Temperature in Fahrenheit: ");
            int temp_f = in.nextInt();
            double temp_c = FahrenheitToCelsius(temp_f);
            System.out.format("%d Fahrenheit = %.2f Celsius\n", temp_f, temp_c); 
        }
        catch (Exception e)
        {
           System.out.println(e.toString());
        }  
    }
    static double FahrenheitToCelsius(int f)
    {
        double t;
  
        t = (f - 32) * (5.0 / 9.0);
  
        return t;
    }
}



 
/*
 
run 1:
 
Enter Temperature in Fahrenheit: 1
1 Fahrenheit = -17.22 Celsius
 
run 2:
  
Enter Temperature in Fahrenheit: 50
50 Fahrenheit = 10.00 Celsius
 
*/
 


answered May 6, 2015 by avibootz
edited Jul 20, 2022 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        int fahrenheit = 96;
       
        double celsius = (fahrenheit - 32) * 5 / 9.0;
 
        System.out.println("Celsius = " + celsius);
    }
}
 
   
   
   
/*
run:
     
Celsius = 35.55555555555556
    
*/

 



answered Jul 22, 2022 by avibootz

Related questions

1 answer 117 views
1 answer 190 views
1 answer 128 views
1 answer 135 views
1 answer 131 views
1 answer 125 views
1 answer 123 views
...