Thursday, August 22, 2019

Java Program to Convert Degree Celsius to Kelvin & Kelvin to Celsius

1. Overview


In this tutorial, We'll learn how to convert Degree Celsius to kelvin in Java programming language. First, we will learn the formula and temperature conversion programs in java.

Degree celsius and kelvin are part of temparature mesures.

The formula for conversion:

K = ( °C + 273.15 )

Where K is kelvin and °C is degree celsius

Example 1:

Celsius: 100
Kelvin: 373.15

Example 2:

Celsius: 120
Kelvin: 393.15

2. Java Program to convert Degree Celsius to Kelvin


Example Program:

package com.java.w3schools.blog.programs.temparature;

/**
 * Java Example to convert Degree Celsius to Kelvin
 * 
 * @author Venkatesh
 *
 */
public class CelsiusToKelvin {

 public static void main(String[] args) {

  float celsiusValue = 100;
  float kelvinValue = convertCelsiusToKelvin(celsiusValue);
  System.out.println("Kelvin value " + kelvinValue + " for Degree Celsius " + celsiusValue);

  celsiusValue = 110;
  kelvinValue = convertCelsiusToKelvin(celsiusValue);
  System.out.println("Kelvin value " + kelvinValue + " for Degree Celsius " + celsiusValue);

  celsiusValue = 120;
  kelvinValue = convertCelsiusToKelvin(celsiusValue);
  System.out.println("Kelvin value " + kelvinValue + " for Degree Celsius " + celsiusValue);

 }

 private static float convertCelsiusToKelvin(float celcius) {

  return (float) (celcius + 273.15);
 }

}

Output:

Kelvin value 373.15 for Degree Celsius 100.0
Kelvin value 383.15 for Degree Celsius 110.0
Kelvin value 393.15 for Degree Celsius 120.0

3. Java Program to convert Kelvin to Degree Celsius


How to go from Kelvin to Celsius?

Formula : °C = K - 273.15

Example program:

package com.java.w3schools.blog.programs.temparature;

/**
 * Java Example to convert Kelvin To Degree Celsius
 * 
 * @author Venkatesh
 *
 */
public class KelvinToCelsius {

 public static void main(String[] args) {

  float celsiusValue = 500;
  float kelvinValue = convertKelvinToCelsius(celsiusValue);
  System.out.println("Degree Celsius value " + kelvinValue + " for Kelvin " + celsiusValue);

  celsiusValue = 400;
  kelvinValue = convertKelvinToCelsius(celsiusValue);
  System.out.println("Degree Celsius value " + kelvinValue + " for Kelvin " + celsiusValue);

  celsiusValue = 300;
  kelvinValue = convertKelvinToCelsius(celsiusValue);
  System.out.println("Degree Celsius value " + kelvinValue + " for Kelvin " + celsiusValue);

 }

 private static float convertKelvinToCelsius(float kelvin) {

  return (float) (kelvin - 273.15);
 }

}

Output:

Degree Celsius value 226.85 for Kelvin 500.0
Degree Celsius value 126.85 for Kelvin 400.0
Degree Celsius value 26.85 for Kelvin 300.0

4. Conclusion


In this article, We have seen the example program on temperature conversions.

Implemented programs on Degree Celsius to Kelvin and Kelvin to Degree Celsius in Java.

GitHub
Temperature Conversion


No comments:

Post a Comment