Hey,
My brother asked me to help him in his homework. His instructor asked him to create a program that will convert Celsius to Fahrenheit and vice versa using Java.
I don't know the formula and I have no knowledge on how to do this. Can you help me?
Thanks
Convert Celsius to Fahrenheit and vice versa
//Conversion in Java Celsius to Fahrenheit and Fahrenheit to Celsius
Formulae:
C / 100 = ( F – 32 ) / 180
Â
import  java.io.* ;
public class Temperature{
// c / 100 = ( f – 32 ) / 180
  public static void main(String args[]){
    InputStreamReader inputstream = new InputStreamReader ( System.in ) ;
    BufferedReader buffer = new BufferedReader( inputstream ) ;
    try {
      //Celsius to Fahrenheit
       System.out.print(" Enter Celsius value for conversion to Fahrenheit: ");
       String txt = buffer.readLine();
       double C=Integer.parseInt( txt );
       double F=( C * 180 / 100 ) + 32;
       System.out.print("Converted Fahrenheit Value: "+F);
       //Farenheit to Celsius
       System.out.print(" n Enter  Fahrenheit value for Conversion to Celsius:  ");
       txt = buffer.readLine();
       F=Integer.parseInt(txt);
       C=( ( F – 32 ) / 180 ) * 100;
       System.out.print("Converted Celsius Value:  " + C);
    }
    catch (IOException  err) {
       System.out.println(" Error reading line from Input board ");
    }
Â
  }
An explanation,
The equation that relates Celsius, Fahrenheit & Kelvin is
c/100=(f-32)/180=(k-273)/100.
here we need only the first part.
c/100=(f-32)/180
This equates in Celsius to c=(f-32)*100/180
and in Fahrenheit to f=(c*180/100) + 32
Thus the system accepts a Celsius value at first and then tries to change into Fahrenheit as shown ion the above formulas and prints the output in a double format, means in fraction.
Next it accepts a Fahrenheit value and tries to solve it using the above formulas and converts to Celsius.
All you get i two values of conversion.
For the input, Input Stream Reader is called and that it may throw the IO exception if the input is not properly read.
Thanks.
Â
Â
Convert Celsius to Fahrenheit and vice versa
Hi Jennifer,
Please see below a simple java program that will convert Celsius to Fahrenheit and Fahrenheit to Celsius. You need to input a temperature and the program will give both conversions.
This demonstrates input and output in Java using Scanner Class.
import java.util.Scanner;
 class TempConverter {
         public static void main(String[] args) {
                  int temp,far,cel;
                  System.out.print("What is the temperature you'd like to convert? ");
                  Scanner myScanner = new Scanner(System.in);
                  temp = myScanner.nextInt();
                  far = temp * 9 / 5 + 32; //use this line if Celsius to Fahrenheit
                  cel = (temp – 32) * 5 / 9; // use this line if Fahrenheit to Celsius
                  System.out.println("Fahrenheit –> Celsius: " + cel); //use this line if Celsius to Fahrenheit
                  System.out.println("Celsius –> Fahrenheit: " + far);// use this line if Fahrenheit to Celsius
         }      Â
}