Posted on by Kalkicode
Code Conversion

# Convert celsius to kelvin

Temperature is a fundamental parameter in physics and daily life. Different regions of the world use different temperature scales. Celsius (°C) and Kelvin (K) are two commonly used temperature scales. Celsius is used in most countries for everyday temperature measurements, while Kelvin is often used in scientific and engineering contexts. Converting temperatures between these two scales is important for various applications, especially in scientific research and technical calculations.

## Problem Statement and Description

The problem is to convert a temperature given in Celsius to its equivalent value in Kelvin. The Kelvin scale is an absolute temperature scale where 0 K is absolute zero, the point at which molecular motion ceases. The conversion formula from Celsius to Kelvin is `kelvin = celsius + 273.15`. This formula adds 273.15 to the Celsius temperature to obtain the equivalent temperature in Kelvin.

## Example

To better understand the problem, let's consider an example. Suppose we have a temperature of 25°C that we want to convert to Kelvin. Using the formula:

``````Kelvin = Celsius + 273.15
Kelvin = 25 + 273.15
Kelvin = 298.15``````

So, 25°C is equivalent to 298.15 K.

## Idea to Solve the Problem

To solve this problem, we need to create a program that takes a temperature in Celsius as input, applies the conversion formula, and outputs the equivalent temperature in Kelvin. 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 celsius_to_kelvin(celsius):
kelvin = celsius + 273.15
return kelvin

main:
celsius_to_kelvin(1)
celsius_to_kelvin(10.80)
celsius_to_kelvin(14.50)``````

## Algorithm Explanation

1. Define the `celsius_to_kelvin` function that takes a parameter `celsius`.
2. Inside the function, calculate the equivalent temperature in Kelvin using the formula `celsius + 273.15`.
3. Return the calculated temperature in Kelvin.
4. In the `main` program, call the `celsius_to_kelvin` function with different test cases: 1, 10.80, and 14.50.
5. Print the results.

## Code Solution

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

//Find the kelvin of given celsius
void celsius_to_kelvin(double celsius)
{
// Formula : celsius + 273.15
// Calculate given celsius to kelvin
double kelvin = celsius + 273.15;
//Display result
printf("Celsius : %lf  Kelvin : %lf\n", celsius, kelvin);
}
int main()
{
//Simple test
celsius_to_kelvin(1);
celsius_to_kelvin(10.80);
celsius_to_kelvin(14.50);
return 0;
}``````

#### Output

``````Celsius : 1.000000  Kelvin : 274.150000
Celsius : 10.800000  Kelvin : 283.950000
Celsius : 14.500000  Kelvin : 287.650000``````
``````/*
C++ program
Convert  celsius to kelvin
*/
#include<iostream>

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

#### Output

``````Celsius : 1 Kelvin : 274.15
Celsius : 10.8 Kelvin : 283.95
Celsius : 14.5 Kelvin : 287.65``````
``````/*
Java program
Convert the celsius to kelvin
*/
class MyMath
{
//Find the kelvin of given celsius
public void celsius_to_kelvin(double celsius)
{
// Formula : celsius + 273.15
// Calculate given celsius to kelvin
double kelvin = celsius + 273.15;
System.out.print("Celsius : " + celsius + " Kelvin : " + kelvin + "\n");
}
public static void main(String[] args)
{
MyMath obj = new MyMath();
//Simple test
obj.celsius_to_kelvin(1);
obj.celsius_to_kelvin(10.80);
obj.celsius_to_kelvin(14.50);
}
}``````

#### Output

``````Celsius : 1.0 Kelvin : 274.15
Celsius : 10.8 Kelvin : 283.95
Celsius : 14.5 Kelvin : 287.65``````
``````/*
C# program
Convert  celsius to kelvin
*/
using System;
class MyMath
{
//Find the kelvin of given celsius
public void celsius_to_kelvin(double celsius)
{
// Formula : celsius + 273.15
// Calculate given celsius to kelvin
double kelvin = celsius + 273.15;
Console.Write("Celsius : " + celsius + " Kelvin : " + kelvin + "\n");
}
public static void Main(String[] args)
{
MyMath obj = new MyMath();
//Simple test
obj.celsius_to_kelvin(1);
obj.celsius_to_kelvin(10.80);
obj.celsius_to_kelvin(14.50);
}
}``````

#### Output

``````Celsius : 1 Kelvin : 274.15
Celsius : 10.8 Kelvin : 283.95
Celsius : 14.5 Kelvin : 287.65``````
``````#   Python 3 program
#   Convert celsius to kelvin

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

def main() :
obj = MyMath()
# Simple test
obj.celsius_to_kelvin(1)
obj.celsius_to_kelvin(10.80)
obj.celsius_to_kelvin(14.50)

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

#### Output

``````Celsius :  1  Kelvin :  274.15
Celsius :  10.8  Kelvin :  283.95
Celsius :  14.5  Kelvin :  287.65``````
``````#   Ruby program
#   Convert celsius to kelvin

class MyMath

# Find the kelvin of given celsius
def celsius_to_kelvin(celsius)

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

obj = MyMath.new()
# Simple test
obj.celsius_to_kelvin(1)
obj.celsius_to_kelvin(10.80)
obj.celsius_to_kelvin(14.50)
end
main()``````

#### Output

``````Celsius : 1 Kelvin : 274.15
Celsius : 10.8 Kelvin : 283.95
Celsius : 14.5 Kelvin : 287.65
``````
``````/*
Scala program
Convert celsius to kelvin
*/
class MyMath
{
//Find the kelvin of given celsius
def celsius_to_kelvin(celsius: Double): Unit = {
// Formula : celsius + 273.15
// Calculate given celsius to kelvin
var kelvin: Double = celsius + 273.15;
print("Celsius : " + celsius + " Kelvin : " + kelvin + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyMath = new MyMath();
//Simple test
obj.celsius_to_kelvin(1);
obj.celsius_to_kelvin(10.80);
obj.celsius_to_kelvin(14.50);
}
}``````

#### Output

``````Celsius : 1.0 Kelvin : 274.15
Celsius : 10.8 Kelvin : 283.95
Celsius : 14.5 Kelvin : 287.65``````
``````/*
Swift program
Convert celsius to kelvin
*/
class MyMath
{
//Find the kelvin of given celsius
func celsius_to_kelvin(_ celsius: Double)
{
// Formula : celsius + 273.15
// Calculate given celsius to kelvin
let kelvin: Double = celsius + 273.15;
print("Celsius : ", celsius ," Kelvin : ", kelvin ,"\n", terminator: "");
}
}
func main()
{
let obj: MyMath = MyMath();
//Simple test
obj.celsius_to_kelvin(1);
obj.celsius_to_kelvin(10.80);
obj.celsius_to_kelvin(14.50);
}
main();``````

#### Output

``````Celsius :  1.0  Kelvin :  274.15
Celsius :  10.8  Kelvin :  283.95
Celsius :  14.5  Kelvin :  287.65``````
``````<?php
/*
Php program
Convert celsius to kelvin
*/
class MyMath
{
//Find the kelvin of given celsius
public	function celsius_to_kelvin(\$celsius)
{
// Formula : celsius + 273.15
// Calculate given celsius to kelvin
\$kelvin = \$celsius + 273.15;
echo "Celsius : ". \$celsius ." Kelvin : ". \$kelvin ."\n";
}
}

function main()
{
\$obj = new MyMath();
//Simple test
\$obj->celsius_to_kelvin(1);
\$obj->celsius_to_kelvin(10.80);
\$obj->celsius_to_kelvin(14.50);
}
main();``````

#### Output

``````Celsius : 1 Kelvin : 274.15
Celsius : 10.8 Kelvin : 283.95
Celsius : 14.5 Kelvin : 287.65``````
``````/*
Node Js program
Convert celsius to kelvin
*/
class MyMath
{
//Find the kelvin of given celsius
celsius_to_kelvin(celsius)
{
// Formula : celsius + 273.15
// Calculate given celsius to kelvin
var kelvin = celsius + 273.15;
process.stdout.write("Celsius : " + celsius + " Kelvin : " + kelvin + "\n");
}
}

function main()
{
var obj = new MyMath();
//Simple test
obj.celsius_to_kelvin(1);
obj.celsius_to_kelvin(10.80);
obj.celsius_to_kelvin(14.50);
}
main();``````

#### Output

``````Celsius : 1 Kelvin : 274.15
Celsius : 10.8 Kelvin : 283.95
Celsius : 14.5 Kelvin : 287.65``````

## Time Complexity

The time complexity of this program is constant for each conversion, as it involves only simple addition. Regardless of the value of the input Celsius temperature, the number of operations remains the same. 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