Fahrenheit to Celsius Conversion in Java

In this article, you will learn how to write Java logic for Fahrenheit to Celsius conversion. The meaning of Fahrenheit to Celsius conversion is the process of converting a temperature value from the Fahrenheit scale to the Celsius scale.

Changing temperatures from Fahrenheit over completely to Celsius is significant in different settings, like weather conditions estimating, logical computations, cooking, and worldwide temperature correlations. It takes into account simple comprehension and correspondence of temperature values across various estimation frameworks.

Fahrenheit scale:

The Fahrenheit scale is regularly utilized in the US and a couple of different nations to quantify temperature. On the Fahrenheit scale, water freezes at 32 degrees Fahrenheit (°F) and bubbles at 212 °F. The scale depends on splitting the reach between the freezing and edges of boiling over water into 180 equivalent parts, known as degrees Fahrenheit.

Celsius scale:

The Celsius scale, also known as the Centigrade scale, is used globally as the standard unit of temperature measurement. On the Celsius scale, water freezes at 0 degrees Celsius (°C) and boils at 100 °C. The scale is based on dividing the range between the freezing and boiling points of water into 100 equal parts, known as degrees Celsius.

Fahrenheit to Celsius Conversion Process

Converting a temperature value from Fahrenheit to Celsius involves using a specific formula to perform the conversion. Below is the formula for Fahrenheit to Celsius conversion.

FORMULA:
Celsius = (Fahrenheit – 32) * 5/9

For example,
let’s convert a Fahrenheit temperature of 86 °F to Celsius:
Celsius = (86 – 32) * 5/9
Celsius = 54 * 5/9
Celsius = 30 °C
Therefore, 86 °F is equivalent to 30 °C.

Fahrenheit to Celsius Conversion Java Programs

Here are a few different solutions for converting a temperature from Fahrenheit to Celsius in Java:

1. Using the formula

In this solution, the formula (Fahrenheit - 32) * 5/9 is used to convert the temperature from Fahrenheit to Celsius. The user is prompted to enter the temperature in Fahrenheit, which is then converted and printed as the temperature in Celsius.

package com.javacodepoint.conversion;

import java.util.Scanner;

public class FahrenheitToCelsius {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		System.out.print("Enter the temperature in Fahrenheit: ");
		double fahrenheit = scanner.nextDouble();
		double celsius = (fahrenheit - 32) * 5 / 9;
		System.out.println("Temperature in Celsius: " + celsius);
	}
}

OUTPUT:

Enter the temperature in Fahrenheit: 97
Temperature in Celsius: 36.111111111111114

2. Using a method

In this solution, a separate method convertFahrenheitToCelsius is defined to perform the conversion. The method takes the temperature in Fahrenheit as input and returns the corresponding temperature in Celsius. The converted temperature is then printed in the main method.

package com.javacodepoint.conversion;

import java.util.Scanner;

public class FahrenheitToCelsius2 {

	public static double convertFahrenheitToCelsius(double fahrenheit) {
		return (fahrenheit - 32) * 5 / 9;
	}

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.print("Enter the temperature in Fahrenheit: ");
		double fahrenheit = scanner.nextDouble();
		double celsius = convertFahrenheitToCelsius(fahrenheit);
		System.out.println("Temperature in Celsius: " + celsius);
	}
}

OUTPUT:

Enter the temperature in Fahrenheit: 100
Temperature in Celsius: 37.77777777777778

3. Using DecimalFormat for formatting

In this solution, the DecimalFormat class is used to format the Celsius temperature with two decimal places. The temperature in Celsius is converted using the formula (Fahrenheit - 32) * 5/9. The DecimalFormat instance is created with the pattern #.## to specify two decimal places, and the formatted Celsius temperature is printed.

package com.javacodepoint.conversion;

import java.text.DecimalFormat;
import java.util.Scanner;

public class FahrenheitToCelsius3 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.print("Enter the temperature in Fahrenheit: ");
		double fahrenheit = scanner.nextDouble();
		double celsius = (fahrenheit - 32) * 5 / 9;
		DecimalFormat decimalFormat = new DecimalFormat("#.##");
		String formattedCelsius = decimalFormat.format(celsius);
		System.out.println("Temperature in Celsius: " + formattedCelsius);
	}
}

OUTPUT:

Enter the temperature in Fahrenheit: 100
Temperature in Celsius: 37.78

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework

Leave a Reply

Your email address will not be published. Required fields are marked *