# Convert Celsius to Fahrenheit and vice versa

Asked By 0 points N/A Posted on -

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

SHARE
Answered By 0 points N/A #101989

## 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.
Â

Â

Answered By 0 points N/A #101990

## 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

Â Â Â Â Â Â Â Â Â  }Â Â Â Â Â Â Â

}