Posted on by Kalkicode
Code Conversion

# Convert kelvin to celsius

Temperature is a fundamental physical quantity that is commonly measured and expressed in various units. Two widely used temperature scales are Kelvin (K) and Celsius (°C). Kelvin is an absolute temperature scale used primarily in scientific contexts, while Celsius is commonly used for everyday temperature measurements. Converting temperatures between these two scales is essential when working with different systems and units of measurement.

## Problem Statement and Description

The problem is to convert a temperature given in Kelvin to its equivalent value in Celsius. Kelvin is an absolute temperature scale where 0 K represents absolute zero, the lowest possible temperature. The conversion formula from Kelvin to Celsius is `celsius = kelvin - 273.15`. This formula subtracts 273.15 from the Kelvin temperature to obtain the equivalent temperature in Celsius.

## Example

To better understand the problem, let's consider an example. Suppose we have a temperature of 300 K that we want to convert to Celsius. Using the formula:

``````Celsius = Kelvin - 273.15
Celsius = 300 - 273.15
Celsius ≈ 26.85``````

So, 300 K is approximately equivalent to 26.85°C.

## Idea to Solve the Problem

To solve this problem, we need to develop a program that takes a temperature in Kelvin as input, applies the conversion formula, and outputs the equivalent temperature in Celsius. We will define a function to perform this conversion, and the main program will call this function for different test cases.

## Pseudocode

Here's the pseudocode for the program:

``````function kelvin_to_celsius(kelvin):
celsius = kelvin - 273.15
return celsius

main:
kelvin_to_celsius(1)
kelvin_to_celsius(1865)
kelvin_to_celsius(280.90)``````

## Algorithm Explanation

1. Define the `kelvin_to_celsius` function that takes a parameter `kelvin`.
2. Inside the function, calculate the equivalent temperature in Celsius using the formula `kelvin - 273.15`.
3. Return the calculated temperature in Celsius.
4. In the `main` program, call the `kelvin_to_celsius` function with different test cases: 1, 1865, and 280.90.
5. Print the results.

## Program Solution

``````//C Program
//Convert kelvin to celsius
#include <stdio.h>

//Find the celsius by given kelvin
void kelvin_to_celsius(double kelvin)
{
// Formula : kelvin - 273.15
// Calculate given kelvin to celsius
double celsius = kelvin - 273.15;
//Display result
printf("Kelvin : %lf  Celsius : %lf\n", kelvin, celsius);
}
int main()
{
//Simple test
kelvin_to_celsius(1);
kelvin_to_celsius(1865);
kelvin_to_celsius(280.90);
return 0;
}``````

#### Output

``````Kelvin : 1.000000  Celsius : -272.150000
Kelvin : 1865.000000  Celsius : 1591.850000
Kelvin : 280.900000  Celsius : 7.750000``````
``````/*
Java program
Convert kelvin to celsius
*/
class MyMath
{
//Find the celsius by given kelvin
public void kelvin_to_celsius(double kelvin)
{
// Formula : kelvin - 273.15
// Calculate given kelvin to celsius
double celsius = kelvin - 273.15;
System.out.print("Kelvin : " + kelvin + " Celsius : " + celsius + "\n");
}
public static void main(String[] args)
{
MyMath obj = new MyMath();
//Simple test
obj.kelvin_to_celsius(1);
obj.kelvin_to_celsius(1865);
obj.kelvin_to_celsius(280.90);
}
}``````

#### Output

``````Kelvin : 1.0 Celsius : -272.15
Kelvin : 1865.0 Celsius : 1591.85
Kelvin : 280.9 Celsius : 7.75``````
``````/*
C# program
Convert kelvin to celsius
*/
using System;
class MyMath
{
//Find the celsius by given kelvin
public void kelvin_to_celsius(double kelvin)
{
// Formula : kelvin - 273.15
// Calculate given kelvin to celsius
double celsius = kelvin - 273.15;
Console.Write("Kelvin : " + kelvin + " Celsius : " + celsius + "\n");
}
public static void Main(String[] args)
{
MyMath obj = new MyMath();
//Simple test
obj.kelvin_to_celsius(1);
obj.kelvin_to_celsius(1865);
obj.kelvin_to_celsius(280.90);
}
}``````

#### Output

``````Kelvin : 1 Celsius : -272.15
Kelvin : 1865 Celsius : 1591.85
Kelvin : 280.9 Celsius : 7.75``````
``````/*
C++ program
Convert kelvin to celsius
*/
#include<iostream>

using namespace std;
class MyMath
{
public:
//Find the celsius by given kelvin
void kelvin_to_celsius(double kelvin)
{
// Formula : kelvin - 273.15
// Calculate given kelvin to celsius
double celsius = kelvin - 273.15;
cout << "Kelvin : " << kelvin << " Celsius : " << celsius << "\n";
}
};
int main()
{
MyMath obj ;
//Simple test
obj.kelvin_to_celsius(1);
obj.kelvin_to_celsius(1865);
obj.kelvin_to_celsius(280.90);
return 0;
}``````

#### Output

``````Kelvin : 1 Celsius : -272.15
Kelvin : 1865 Celsius : 1591.85
Kelvin : 280.9 Celsius : 7.75``````
``````<?php
/*
Php program
Convert kelvin to celsius
*/
class MyMath
{
//Find the celsius by given kelvin
public	function kelvin_to_celsius(\$kelvin)
{
// Formula : kelvin - 273.15
// Calculate given kelvin to celsius
\$celsius = \$kelvin - 273.15;
echo "Kelvin : ". \$kelvin ." Celsius : ". \$celsius ."\n";
}
}

function main()
{
\$obj = new MyMath();
//Simple test
\$obj->kelvin_to_celsius(1);
\$obj->kelvin_to_celsius(1865);
\$obj->kelvin_to_celsius(280.90);
}
main();``````

#### Output

``````Kelvin : 1 Celsius : -272.15
Kelvin : 1865 Celsius : 1591.85
Kelvin : 280.9 Celsius : 7.75``````
``````/*
Node Js program
Convert kelvin to celsius
*/
class MyMath
{
//Find the celsius by given kelvin
kelvin_to_celsius(kelvin)
{
// Formula : kelvin - 273.15
// Calculate given kelvin to celsius
var celsius = kelvin - 273.15;
process.stdout.write("Kelvin : " + kelvin + " Celsius : " + celsius + "\n");
}
}

function main()
{
var obj = new MyMath();
//Simple test
obj.kelvin_to_celsius(1);
obj.kelvin_to_celsius(1865);
obj.kelvin_to_celsius(280.90);
}
main();``````

#### Output

``````Kelvin : 1 Celsius : -272.15
Kelvin : 1865 Celsius : 1591.85
Kelvin : 280.9 Celsius : 7.75``````
``````#   Python 3 program
#   Convert kelvin to celsius

class MyMath :
# Find the celsius by given kelvin
def kelvin_to_celsius(self, kelvin) :
#  Formula : kelvin - 273.15
#  Calculate given kelvin to celsius
celsius = kelvin - 273.15
print("Kelvin : ", kelvin ," Celsius : ", celsius ,"\n", end = "")

def main() :
obj = MyMath()
# Simple test
obj.kelvin_to_celsius(1)
obj.kelvin_to_celsius(1865)
obj.kelvin_to_celsius(280.90)

if __name__ == "__main__": main()``````

#### Output

``````Kelvin :  1  Celsius :  -272.15
Kelvin :  1865  Celsius :  1591.85
Kelvin :  280.9  Celsius :  7.75``````
``````#   Ruby program
#   Convert kelvin to celsius

class MyMath

# Find the celsius by given kelvin
def kelvin_to_celsius(kelvin)

#  Formula : kelvin - 273.15
#  Calculate given kelvin to celsius
celsius = kelvin - 273.15
print("Kelvin : ", kelvin ," Celsius : ", celsius ,"\n")
end
end
def main()

obj = MyMath.new()
# Simple test
obj.kelvin_to_celsius(1)
obj.kelvin_to_celsius(1865)
obj.kelvin_to_celsius(280.90)
end
main()``````

#### Output

``````Kelvin : 1 Celsius : -272.15
Kelvin : 1865 Celsius : 1591.85
Kelvin : 280.9 Celsius : 7.75
``````
``````/*
Scala program
Convert kelvin to celsius
*/
class MyMath
{
//Find the celsius by given kelvin
def kelvin_to_celsius(kelvin: Double): Unit = {
// Formula : kelvin - 273.15
// Calculate given kelvin to celsius
var celsius: Double = kelvin - 273.15;
print("Kelvin : " + kelvin + " Celsius : " + celsius + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyMath = new MyMath();
//Simple test
obj.kelvin_to_celsius(1);
obj.kelvin_to_celsius(1865);
obj.kelvin_to_celsius(280.90);
}
}``````

#### Output

``````Kelvin : 1.0 Celsius : -272.15
Kelvin : 1865.0 Celsius : 1591.85
Kelvin : 280.9 Celsius : 7.75``````
``````/*
Swift program
Convert kelvin to celsius
*/
class MyMath
{
//Find the celsius by given kelvin
func kelvin_to_celsius(_ kelvin: Double)
{
// Formula : kelvin - 273.15
// Calculate given kelvin to celsius
let celsius: Double = kelvin - 273.15;
print("Kelvin : ", kelvin ," Celsius : ", celsius ,"\n", terminator: "");
}
}
func main()
{
let obj: MyMath = MyMath();
//Simple test
obj.kelvin_to_celsius(1);
obj.kelvin_to_celsius(1865);
obj.kelvin_to_celsius(280.90);
}
main();``````

#### Output

``````Kelvin :  1.0  Celsius :  -272.15
Kelvin :  1865.0  Celsius :  1591.85
Kelvin :  280.9  Celsius :  7.75``````

## Time Complexity

The time complexity of this program is constant for each conversion, as it involves simple arithmetic subtraction. The number of operations remains the same regardless of the input Kelvin temperature. Thus, the time complexity is O(1).

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post