Convert Celsius to Fahrenheit and vice versa

Asked By 0 points N/A Posted on -
qa-featured

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
Best Answer by Sheldon Ron
Best Answer
Best Answer
Answered By 0 points N/A #101989

Convert Celsius to Fahrenheit and vice versa

qa-featured

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

qa-featured

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

          }       

}

Login/Register to Answer

Related Questions