In the previous article, we have discussed about Java Program to Convert Acer to Square Kilometer and Square Kilometer to Acer
In this article we will see how to convert Celsius to Kelvin and Kelvin to Celsius by using Java programming language.
Java Program to Convert Celsius to kelvin and Kelvin to Celsius
Before jumping into the program let’s know the relationship between Celsius to Kelvin and how we can convert Celsius to Kelvin and vice versa.
Generally Celsius and Kelvin are used as unit in case of temperature measurement.
1 Celsius = 274.15 Kelvin 1 Kelvin = -272.15 Celsius
Formula to convert Celsius to Kelvin.
Kelvin = Celsius + 273.15
Formula to convert Kelvin to Celsius.
Celsius = Kelvin - 273.15
Let’s see different ways to convert Celsius to Kelvin and Kelvin to Celsius.
Method-1: Java Program to Convert Celsius to kelvin and Kelvin to Celsius By Using Static Input Value
Approach:
- Declare Celsius and Kelvin value.
- Then convert Celsius to Kelvin and Kelvin to Celsius by using the formula.
- Print result.
Program:
public class Main { public static void main(String args[]) { //value of kelvin declared double kelvin = 1; //value of celsius declared double celsius = 1; //converting celsius to kelvin double k = celsius + 273.15; //converting kelvin to celsius double c = kelvin - 273.15; //printing result System.out.println("Value of "+kelvin+" kelvin in celsius: "+ c); System.out.println("Value of "+celsius+" celsius in kelvin: "+ k); } }
Output: Value of 1.0 kelvin in celsius: -272.15 Value of 1.0 celsius in kelvin: 274.15
Method-2: Java Program to Convert Celsius to kelvin and Kelvin to Celsius By Using User Input Value
Approach:
- Take user input of Celsius and Kelvin value.
- Then convert Celsius to Kelvin and Kelvin to Celsius by using the formula.
- Print result.
Program:
import java.util.*; public class Main { public static void main(String args[]) { //Scanner class object created Scanner sc=new Scanner(System.in); //Taking the value input of double variable celsius System.out.println("Enter value of celsius: "); double celsius = sc.nextDouble(); //Taking the value input of double variable kelvin System.out.println("Enter value of kelvin: "); double kelvin = sc.nextDouble(); //converting celsius to kelvin double k = celsius + 273.15; //converting kelvin to celsius double c = kelvin - 273.15; //printing result System.out.println("Value of "+kelvin+" kelvin in celsius: "+ c); System.out.println("Value of "+celsius+" celsius in kelvin: "+ k); } }
Output: Enter value of celsius: 15 Enter value of kelvin: 5 Value of 5.0 kelvin in celsius: -268.15 Value of 15.0 celsius in kelvin: 288.15
Method-3: Java Program to Convert Celsius to kelvin and Kelvin to Celsius By Using User Defined Method
Approach:
- Take user input of Celsius and Kelvin value.
- Call a user defined method by passing Celsius and Kelvin value as parameter.
- Inside method convert Celsius to Kelvin and vice versa by using the formula.
- Print result.
Program:
import java.util.*; public class Main { public static void main(String args[]) { //Scanner class object created Scanner sc=new Scanner(System.in); //Taking the value input of double variable celsius System.out.println("Enter value of celsius: "); double celsius = sc.nextDouble(); //Taking the value input of double variable kelvin System.out.println("Enter value of kelvin: "); double kelvin = sc.nextDouble(); //calling user defined method convert() convert(celsius, kelvin); } //convert() method to convert Celsius to Kelvin and vice versa public static void convert(double celsius, double kelvin) { //converting celsius to kelvin double k = celsius + 273.15; //converting kelvin to celsius double c = kelvin - 273.15; //printing result System.out.println("Value of "+kelvin+" kelvin in celsius: "+ c); System.out.println("Value of "+celsius+" celsius in kelvin: "+ k); } }
Output: Enter value of celsius: 20 Enter value of kelvin: 200 Value of 200.0 kelvin in celsius: -73.14999999999998 Value of 20.0 celsius in kelvin: 293.15
Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples
Related Java Programs: