Write a Java program to input Celsius from user and convert temperature from Celsius to Fahrenheit. How to convert Celsius to Fahrenheit in Java.
In this on going Java programming examples, we will continue to enhance basic Java programming skills. In this post we will learn to convert given temperature from Celsius to Fahrenheit in Java.
Example
Input
Input
Enter temperature in Celsius : 28.5
Output
28.5 degree Celsius is equal to 83.3 degree Fahrenheit.
Required knowledge
Arithmetic operators, Data types, Basic Input/Output
How to convert temperature from Celsius to Fahrenheit
Before we write logic in Java program, let us learn how we can convert Celsius to Fahrenheit mathematically.
We know 0° Celsius is equal to 32° Fahrenheit.
Equation to convert temperature from Celsius to Fahrenheit is given by:
F = C * (9/5) + 32
or
F = C * 1.8 + 32
Here, C is temperature in Celsius.
Program to convert Celsius to Fahrenheit
/**
* Java program to convert temperature from Celsius to Fahrenheit.
*/
import java.util.Scanner;
public class TemperatureConverter {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
/* Input temperature in Celsius from user */
System.out.print("Enter temperature in Celsius: ");
float C = in.nextFloat();
/* Convert Celsius to Fahrenheit */
float F = C * (9f / 5) + 32;
/* Print result */
System.out.println(C + " degree Celsius is equal to " + F +" degree Fahrenheit.");
}
}
Output
Enter temperature in Celsius: 28.5 28.5 degree Celsius is equal to 83.3 degree Fahrenheit.
Happy coding 😉