Convert Fahrenheit to Celsius and Vice Versa in Java

In Java, to convert a given input value from Fahrenheit to Celsius and vice versa (temperature conversion), we can use an if-else conditional statement. In this article, we will focus on learning through examples, algorithms, logic, and program explanations for better understanding.

Required Knowledge

  • Java programming basics
  • Java Operators
  • Conditional Statements
  • Ternary Operator

Problem Statement

We need to write a Java program to convert a given input from Fahrenheit to Celsius and vice versa. The program should take a double value as input and print the result after converting it to either Celsius or Fahrenheit. This above action should be done in our program. Let’s look at an example,

Example 1:

If the input temperature is 30 F, then the output should be “-1.0 degree C”.

Explanation:

Input temperature:  30

After performing calculation: (value - 32) * 5 / 9;

Hence, the result is -1.0 degree C

Java Program to convert Fahrenheit to Celsius and Vice – Versa

In the below program we are using if-else conditional statement to convert fahrenheit to celsius and vice versa. The program gives two choices either 1 or 2 to choose. If we select one, it will convert fahrenheit to celsius and using Math.round() method it will round the result. Otherwise, if the user chooses option 2, it will convert celsius to fahrenheit and round the result using Math.round() method.

import java.util.Scanner;

public class FahrenheitCelsius {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);

		System.out.println("Fahrenheit to Celsius 1 or Celsius to Fahrenheit 2");
		int temp = s.nextInt();
		if (temp == 1) {

			System.out.println("You have choosen Fahrenheit to Celsius");
			System.out.println("Enter Fahrenheit value");
			double value = s.nextDouble();
			double result = (value - 32) * 5 / 9;
			System.out.println("Celsius is:");
			System.out.println(Math.round(result) * 10.0 / 10.0 + " degrees C");
		} else if (temp == 2) {

			System.out.println("You have choosen Celsius to Fahrenheit");
			System.out.println("Enter Celsius value");
			double value1 = s.nextDouble();
			double result1 = (value1 * 9 / 5) + 32;
			System.out.println("Fahrenheit is:");
			System.out.println(Math.round(result1) * 10.0 / 10.0 + " degrees F");
		} else {
			System.out.println("Not a valid choice");
		}
		s.close();

	}

}

Output:

Fahrenheit to Celsius 1 or Celcius to Fahrenheit 2
2
You have chosen Celsius to Fahrenheit
Enter Celsius value
24
Fahrenheit is:
75.0 degrees F

Java program Explanation to convert from Fahrenheit to Celsius and vice-versa

  • The java program asks the user to choose between converting from Fahrenheit to Celsius or from Celsius to Fahrenheit.
  • Then it reads the user’s choice using a Scanner object.
  • If the user chooses Fahrenheit to Celsius:
    • Program asks the user to enter a Fahrenheit value.
    • It calculates the Celsius value using the conversion formula.
    • Prints the Celsius value rounded to one decimal place.
  • Otherwise, if the user chooses Celsius to Fahrenheit:
    • Program asks the user to enter a Celsius value.
    • It calculates the Fahrenheit value using the conversion formula.
    • Prints the Fahrenheit value rounded to one decimal place.
  • If the user chooses neither option 1 nor 2, it prints “Not a valid choice”.
  • It closes the Scanner object to release system resources.

Conclusion

In this article we explored using conditional statements (if-else) to convert temperature from fahrenheit to celsius and celsius to fahrenheit. We understood the basic logic to do this and moved on to perform java program.

For more information on related topics, you can find these articles which might be helpful:-

We hope this tutorial has been helpful. If you have any queries or need any further clarification, feel free to comment below. Don’t forget to subscribe our newsletter for more Java Programming tips and tutorials.

Happy Learning!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.