Here, we are going to see how to write a Java Program to convert Celsius to Fahrenheit( temperature conversion java). Both Celsius and Fahrenheit are scales of temperature. It is often required to convert one unit to another.

Formula to converting Fahrenheit to Celsius- We will be using this formula to make our java program to convert Celsius to Fahrenheit and vice versa using methods.

## Write a Program to Convert Celsius to Fahrenheit in Java

import java.util.Scanner;

public class CelsiusToFahrenheit {
static void convert() {
Scanner sc = new Scanner(System.in);
System.out.println("enter value in Celcius: ");
double cel = sc.nextDouble();
double fa = ((9.0/5)*cel)+32;
System.out.println("temperature in Fahrenheit: "+fa);
}
public static void main(String[] args) {
convert();
}
}


OUTPUT

In line 4, we define the convert() method. When called, first we create an Object of Scanner class to accept console-based user input. We name this object ‘sc’. You can choose your own name. Notice carefully in the Output: A warning is being displayed beside the Scanner object declaration statement!. This says that the Scanner object is not closed. To avoid this simply type “sc.close()” at the end of the method.

Subsequently, we ask the user to input the temperature value in Celsius and store it in a double type variable called ‘cel’. Then we use the formulae to convert it to Fahrenheit. Finally, display it in the console using System.out.println.

After completing the method definition for convert, we simply call it from the main method. As we know execution starts from the main method in Java, so in the first line of the main method the compiler calls the static method ‘convert()’ and our defined method takes it from there. Since the convert() method is static, no Object creation is required to call it.

This is the simplest way to achieve the conversion in Java. we use double to handle fractions.

Well, we can also define Celsius and Fahrenheit as a separate Class! Using the power of Object-Oriented programming that is also possible in Java!

### Temperature Class java Celsius & Fahrenheit

First, we shall define a class called Celsius. Celsius can hold a double(decimal) value called temp. It also contains a method that can convert Celsius to Fahrenheit. Similarly, Fahrenheit is also a class that can hold a temperature value and a method to convert it to Celsius.

Celsius

public class Celsius {
double temp;
public Celsius(double temp) {
this.temp = temp;
}
Fahrenheit toFahrenheit() {
double fa = ((9.0/5)*temp)+32;
Fahrenheit f = new Fahrenheit(fa);
return f;
}
}


Fahrenheit

public class Fahrenheit {
double temp;
public Fahrenheit(double temp) {
this.temp = temp;
}
Celsius toCelsius() {
double cel =(temp-32)*(5.0/9);
Celsius c = new Celsius(cel);
return c;
}
}


Now, we use the Classes and their methods by creating their objects in the main method.

### Java Temperature Convert Method( C to F)

public class Converter {
public static void main(String[] args) {
Celsius c = new Celsius(0);
Fahrenheit f = c.toFahrenheit();
System.out.println("In Celsius: "+c.temp+" | In Fahrenheit: "+ f.temp);
}
}

OUTPUT

To sum up, Java programs can be used to convert Fahrenheit to Celsius just by using the same formula.

GUI application can be made using Java swing. If you need temperature conversion using GUI, then feel free to contact us.  We can help you with the java GUI temperature converter.

Also, if you are looking for Java Programming Homework Help then check our Java help services. Please share our tutorial(how to convert Celsius into Fahrenheit in java) if you liked it. Cheers!!