# Celsius to Fahrenheit Conversion in Java

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

Converting temperatures from Celsius to Fahrenheit is important in various contexts, such as weather forecasting, scientific calculations, and international temperature comparisons. It allows for easy understanding and communication of temperature values across different measurement systems.

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 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 to Fahrenheit Conversion Process

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

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

For example,
let’s convert a Celsius temperature of 20 °C to Fahrenheit:
Fahrenheit = (20 * 9/5) + 32
Fahrenheit = 36 + 32
Fahrenheit = 68 °F

Therefore, 20 °C is equivalent to 68 °F.

## Celsius to Fahrenheit Conversion Java Programs

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

### 1. Using the formula

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

```package com.javacodepoint.conversion;

import java.util.Scanner;

public class CelsiusToFahrenheit {

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

```

OUTPUT:

Enter the temperature in Celsius: 36
Temperature in Fahrenheit: 96.8

### 2. Using a method

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

```package com.javacodepoint.conversion;

import java.util.Scanner;

public class CelsiusToFahrenheit2 {

public static double convertCelsiusToFahrenheit(double celsius) {
return (celsius * 9 / 5) + 32;
}

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

```

OUTPUT:

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

### 3. Using BigDecimal

In this solution, the temperature in Fahrenheit is calculated using the formula `(Celsius * 9/5) + 32`. The `BigDecimal` class is used to handle decimal precision. The Fahrenheit temperature is stored in an `BigDecimal` object and printed.

```package com.javacodepoint.conversion;

import java.math.BigDecimal;
import java.util.Scanner;

public class CelsiusToFahrenheit3 {

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

```

OUTPUT:

Enter the temperature in Celsius: 40
Temperature in Fahrenheit: 104.0

### 4. Using DecimalFormat for formatting

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

```package com.javacodepoint.conversion;

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

public class CelsiusToFahrenheit4 {

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

```

OUTPUT:

Enter the temperature in Celsius: 32
Temperature in Fahrenheit: 89.6