# Temperature Conversions using Java

In this article, we’ll see the temperature conversions between four different temperature scales named Fahrenheit, Celsius, Kelvin, and Rankine.

## Temperature Scales

### Fahrenheit Scale

• Fahrenheit scale was created by German scientist Daniel Gabriel Fahrenheit
• According to this scale, water freezes at 32 degrees of Fahrenheit and boils at 212 degrees of Fahrenheit
• So it will take 180 degrees of Fahrenheit to boil water from its freezing state

### Celsius Scale

• Celsius scale was created by Swedish scientist Anders Celsius
• According to this scale, water freezes at 0 degrees of Celsius and boils at 100 degrees of Celsius
• So it will take 100 degrees Celsius to boil water from its freezing state

### Kelvin Scale

• In the 19th century, the Kelvin scale was created by the British scientist William Thompson, who is also called as Lord Kelvin
• According to the Kelvin scale, water freezes at 273.15 K and boils at 373.15 K
• So it will take 100 K to boil water from its freezing state

### Rankine Scale

• In the 19th century, the Rankine Scale was created by the Scottish scientist William John Rankine
• According to Rankine Scale, water freezes at 491.67 degrees Rankine and boils at 671.67 degrees Rankine
• So it will take 180 degrees of Rankine to boil water from its freezing state

## Temperature Conversions

### Convert Celsius to Fahrenheit:

• To convert Celsius to Fahrenheit
• Multiply the ratio of Fahrenheit to Celsius
• 100 degrees of Celsius is equaled to 180 degrees of Fahrenheit to boil the water from its frozen state
• The ratio of Fahrenheit to Celsius can be written as 180/100, reducing it to 9/5
• Add the freezing temperature of Celsius, that is, 32 ºF
• Formula to convert Celsius to Rankine: ºR = ºC * 9/5 + 32
``````	public static double celsiusToFahrenheit(double celsius) {
return (celsius * 9 / 5) + 32;
}``````

### Convert Celsius to Kelvin:

• To convert Celsius to Kelvin, we just need to add freezing temperature of Kelvin to ºC
• Formula to convert Celsius to Kelvin: K = ºC + 273.15 K
``````	public static double celsiusToKelvin(double celsius) {
return celsius + 273.15;
}``````

### Convert Celsius to Rankine:

• To convert Celsius to Rankine
• Multiply the ratio of Rankine to Celsius
• 100 degrees of Celsius is equaled to 180 degrees of Rankine to boil the water from its frozen state
• The ratio of Rankine to Celsius can be written as 180/100, reducing it to 9/5
• Add the freezing temperature of Rankine, that is, 491.67 ºR
• Formula to convert Celsius to Rankine: ºR = ºC * 9/5 + 491.67
``````	public static double celsiusToRankine(double celsius) {
return (celsius * 9 / 5) + 491.67;
}``````

### Convert Fahrenheit to Celsius:

• To convert Fahrenheit to Celsius
• Reduce the freezing temperature of Fahrenheit, that is, 32 from given Fahrenheit degrees
• Multiply the ratio of Celsius to Fahrenheit
• 100 degrees of Celsius is equal to 180 degrees of Fahrenheit to boil the water from its frozen state
• The ratio of Celsius to Fahrenheit can be written as 100/180, reducing it to 5/9
• Formula to convert Fahrenheit to Celsius: ºC = (ºF – 32) * 5/9
``````	public static double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}``````

### Convert Fahrenheit to Kelvin

• To convert Fahrenheit to Kelvin
• Reduce the freezing temperature of Fahrenheit, that is, 32 from given Fahrenheit degrees
• Multiply the ratio of Kelvin to Fahrenheit
• 100 degrees of Kelvin is equaled to 180 degrees of Fahrenheit to boil the water from its frozen state
• The ratio of Kelvin to Fahrenheit can be written as 100/180, reducing it to 5/9
• Add the freezing temperature of Kelvin, that is, 273.15 K
• Formula to convert Fahrenheit to Kelvin: K = (ºF – 32) * 5/9 + 273.15
``````	public static double fahrenheitToKelvin(double fahrenheit) {
return (fahrenheit - 32) * 5 / 9 + 273.15;
}``````

### Convert Fahrenheit to Rankine

• To convert Fahrenheit to Rankine
• Subtract the freezing temperature of Fahrenheit, that is, 32 from the given Fahrenheit degrees
• Add freezing temperature of Rankine, that is, 491.67
• Formula to convert Fahrenheit to Rankine: ºR = (ºF – 32) + 491.67
``````	public static double fahrenheitToRankine(double fahrenheit) {
return (fahrenheit - 32) + 491.67;
}``````

### Convert Kelvin to Celsius

• To convert Kelvin to Celsius, we just need to subtract the freezing temperature of Kelvin, that is, 273.15 from given Kelvin
• Formula to convert Kelvin to Celsius: ºC = K – 273.15
``````	public static double kelvinToCelsius(double kelvin) {
return kelvin - 273.15;
}``````

### Convert Kelvin to Fahrenheit

• To convert Kelvin to Fahrenheit
• Reduce the freezing temperature of Kelvin, that is, 273.15 from Kelvin degrees
• Multiply the ratio of Fahrenheit to Kelvin
• 100 degrees of Kelvin is equal to 180 degrees of Fahrenheit to boil the water from its frozen state
• The ratio of Fahrenheit to Kelvin can be written as 180/100, reducing it to 9/5
• Add the freezing temperature of Fahrenheit, that is, 32 ºF
• Formula to convert Kelvin to Rankine: ºR = (K – 273.15) * 9/5 + 32
``````	public static double kelvinToFahrenheit(double kelvin) {
return (kelvin - 273.15) * 9 / 5 + 32;
}``````

### Convert Kelvin to Rankine

• To convert Kelvin to Rankine
• Reduce the freezing temperature of Kelvin, that is, 273.15 from Kelvin degrees
• Multiply the ratio of Rankine to Kelvin
• 100 degrees of Kelvin is equaled to 180 degrees of Rankine to boil the water from its frozen state
• The ratio of Rankine to Kelvin can be written as 180/100, reducing it to 9/5
• Add the freezing temperature of Rankine, that is, 491.67 ºR
• Formula to convert Kelvin to Rankine: ºR = (K – 273.15) * 9/5 + 491.67
``````	public static double kelvinToRankine(double kelvin) {
return (kelvin - 273.15) * 9 / 5 + 491.67;
}``````

### Convert Rankine to Celsius

• To convert Rankine to Celsius
• Reduce the freezing temperature of Rankine, that is, 491.67 from given Rankine degrees
• Multiply the ratio of Celsius to Rankine
• 100 degrees of Celsius is equal to 180 degrees of Rankine to boil the water from its frozen state
• The ratio of Celsius to Rankine can be written as 100/180, reducing it to 5/9
• Formula to convert Rankine to Celsius: ºC = (ºR – 491.67) * 5/9
``````	public static double rankineToCelsius(double rankine) {
return (rankine - 491.67) * 5 / 9;
}``````

### Convert Rankine to Fahrenheit

• To convert Rankine to Fahrenheit
• Subtract the freezing temperature of Rankine, that is, 491.67 from the given Rankine degrees
• Add freezing temperature of Fahrenheit, that is, 32
• Formula to convert Rankine to Fahrenheit: ºF = (ºR – 491.67) + 32
``````	public static double rankineToFahrenheit(double rankine) {
return (rankine - 491.67) + 32;
}``````

### Convert Rankine to Kelvin

• To convert Rankine to Kelvin
• Reduce the freezing temperature of Rankine, that is, 491.67 from the given Rankine degrees
• Multiply the ratio of Kelvin to Rankine
• 100 degrees of Kelvin is equaled to 180 degrees of Rankine to boil the water from its frozen state
• The ratio of Kelvin to Rankine can be written as 100/180, reducing it to 5/9
• Add the freezing temperature of Kelvin, that is, 273.15 K
• Formula to convert Rankine to Kelvin: K = (ºR – 491.67) * 5/9 + 273.15
``````	public static double rankineToKelvin(double rankine) {
return (rankine - 491.67) * 5 / 9 + 273.15;
}``````

## Full Example

Below is the complete example of calling all these functions in a main function.

``````package com.techstackjournal;

import java.util.Scanner;

public class TemperatureConversions {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter a number to convert the temperature:");
double number = scanner.nextDouble();
scanner.close();

System.out.printf("\n %.2f Celsius to Fahrenheit = %.2f", number, celsiusToFahrenheit(number));
System.out.printf("\n %.2f Celsius to Kelvin = %.2f", number, celsiusToKelvin(number));
System.out.printf("\n %.2f Celsius to Rankine = %.2f", number, celsiusToRankine(number));
System.out.printf("\n %.2f Fahrenheit to Celsius = %.2f", number, fahrenheitToCelsius(number));
System.out.printf("\n %.2f Fahrenheit to Kelvin = %.2f", number, fahrenheitToKelvin(number));
System.out.printf("\n %.2f Fahrenheit to Rankine = %.2f", number, fahrenheitToRankine(number));
System.out.printf("\n %.2f Kelvin to Celsius = %.2f", number, kelvinToCelsius(number));
System.out.printf("\n %.2f Kelvin to Fahrenheit = %.2f", number, kelvinToFahrenheit(number));
System.out.printf("\n %.2f Kelvin to Rankine = %.2f", number, kelvinToRankine(number));
System.out.printf("\n %.2f Rankine to Celsius = %.2f", number, rankineToCelsius(number));
System.out.printf("\n %.2f Rankine to Fahrenheit = %.2f", number, rankineToFahrenheit(number));
System.out.printf("\n %.2f Rankine to Kelvin = %.2f", number, rankineToKelvin(number));

}

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

public static double celsiusToKelvin(double celsius) {
return celsius + 273.15;
}

public static double celsiusToRankine(double celsius) {
return (celsius * 9 / 5) + 491.67;
}

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

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

public static double fahrenheitToRankine(double fahrenheit) {
return (fahrenheit - 32) + 491.67;
}

public static double kelvinToCelsius(double kelvin) {
return kelvin - 273.15;
}

public static double kelvinToFahrenheit(double kelvin) {
return (kelvin - 273.15) * 9 / 5 + 32;
}

public static double kelvinToRankine(double kelvin) {
return (kelvin - 273.15) * 9 / 5 + 491.67;
}

public static double rankineToCelsius(double rankine) {
return (rankine - 491.67) * 5 / 9;
}

public static double rankineToFahrenheit(double rankine) {
return (rankine - 491.67) + 32;
}

public static double rankineToKelvin(double rankine) {
return (rankine - 491.67) * 5 / 9 + 273.15;
}

}``````

Output:

``````Enter a number to convert the temperature:
10

10.00 Celsius to Fahrenheit = 50.00
10.00 Celsius to Kelvin = 283.15
10.00 Celsius to Rankine = 509.67
10.00 Fahrenheit to Celsius = -12.22
10.00 Fahrenheit to Kelvin = 260.93
10.00 Fahrenheit to Rankine = 469.67
10.00 Kelvin to Celsius = -263.15
10.00 Kelvin to Fahrenheit = -441.67
10.00 Kelvin to Rankine = 18.00
10.00 Rankine to Celsius = -267.59
10.00 Rankine to Fahrenheit = -449.67
10.00 Rankine to Kelvin = 5.56``````