Write a Java program to input temperature in degree Fahrenheit and convert it to degree Centigrade. How to convert temperature from Fahrenheit to Celsius in Java programming. Java program for temperature conversion. Logic to convert temperature from Fahrenheit to Celsius in Java program.
Required knowledge
Arithmetic operators, Variables and expressions, Data types, Data types
Temperature conversion formula
Formula to convert temperature from degree Fahrenheit to degree Celsius is given by -
Logic to convert temperature from Fahrenheit to Celsius
Step by step descriptive logic to convert temperature from degree Fahrenheit to degree Celsius -
Input temperature in fahrenheit from user. Store it in some variable say fahrenheit.
Apply the temperature conversion formula celsius = (fahrenheit - 32) * 5 / 9.
Print the value of celsius.
Program to convert temperature from fahrenheit to celsius
/** * Java program to convert temperature from degree fahrenheit to celsius */ import java.util.Scanner; class Test { public static void main(String args[]) { double celsius, fahrenheit; Scanner op=new Scanner(System.in); /* Input temperature in fahrenheit */ System.out.print("Enter temperature in Fahrenheit: "); fahrenheit=op.nextDouble(); /* Fahrenheit to celsius conversion formula */ celsius = (fahrenheit - 32) * 5 / 9; /* Print the value of celsius */ System.out.println(fahrenheit+" Fahrenheit = "+celsius+" Celsius"); } }
Output:
Enter temperature in Fahrenheit: 87.8 87.8 Fahrenheit = 31.0 Celsius