Posted on by Kalkicode
Code Conversion

Convert fahrenheit to kelvin

Temperature conversion is a fundamental task in various scientific, engineering, and everyday contexts. Different regions and fields use different temperature scales, such as Fahrenheit (°F) and Kelvin (K). Fahrenheit is commonly used in the United States for temperature measurements, while Kelvin is an absolute temperature scale often used in scientific research and calculations. Converting temperatures between Fahrenheit and Kelvin is essential when working with different units of measurement.

Problem Statement and Description

The problem is to convert a temperature given in Fahrenheit to its equivalent value in Kelvin. Kelvin is an absolute temperature scale with 0 K representing absolute zero, the lowest possible temperature. The conversion formula from Fahrenheit to Kelvin is kelvin = (fahrenheit - 32) × 5/9 + 273.15. This formula first subtracts 32 from the Fahrenheit temperature to get the equivalent temperature in Celsius, then multiplies by 5/9, and finally adds 273.15 to obtain the temperature in Kelvin.

Example

Let's consider an example to understand the problem. Suppose we have a temperature of 98.6°F (normal human body temperature) that we want to convert to Kelvin. Using the formula:

Kelvin = (Fahrenheit - 32) × 5/9 + 273.15
Kelvin = (98.6 - 32) × 5/9 + 273.15
Kelvin ≈ 310.15

So, 98.6°F is approximately equivalent to 310.15 K.

Idea to Solve the Problem

To solve this problem, we need to create a program that takes a temperature in Fahrenheit 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 fahrenheit_to_kelvin(fahrenheit):
    kelvin = (fahrenheit - 32) × 5/9 + 273.15
    return kelvin

main:
    fahrenheit_to_kelvin(1)
    fahrenheit_to_kelvin(14.5)
    fahrenheit_to_kelvin(52.9)

Algorithm Explanation

  1. Define the fahrenheit_to_kelvin function that takes a parameter fahrenheit.
  2. Inside the function, calculate the equivalent temperature in Kelvin using the formula (fahrenheit - 32) × 5/9 + 273.15.
  3. Return the calculated temperature in Kelvin.
  4. In the main program, call the fahrenheit_to_kelvin function with different test cases: 1, 14.5, and 52.9.
  5. Print the results.

Code Solution

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

//Find the kelvin by given fahrenheit
void fahrenheit_to_kelvin(double fahrenheit)
{
	// Formula :  (fahrenheit - 32) × 5/9 + 273.15 
	// Calculate given fahrenheit to kelvin 
	double kelvin = (fahrenheit - 32) * (5 / 9.0) + 273.15;
	//Display result
	printf("Fahrenheit : %lf  Kelvin : %lf\n", fahrenheit, kelvin);
}
int main()
{
	//Simple test
	fahrenheit_to_kelvin(1);
	fahrenheit_to_kelvin(14.5);
	fahrenheit_to_kelvin(52.9);
	return 0;
}

Output

Fahrenheit : 1.000000  Kelvin : 255.927778
Fahrenheit : 14.500000  Kelvin : 263.427778
Fahrenheit : 52.900000  Kelvin : 284.761111
/*
  Java program
  Convert fahrenheit to kelvin
*/
class MyMath
{
	//Find the kelvin by given fahrenheit
	public void fahrenheit_to_kelvin(double fahrenheit)
	{
		// Formula : (fahrenheit - 32) × 5/9 + 273.15 
		// Calculate given fahrenheit to kelvin 
		double kelvin = (fahrenheit - 32) * (5 / 9.0) + 273.15;
		System.out.print("Fahrenheit : " + fahrenheit + " Kelvin : " + kelvin + "\n");
	}
	public static void main(String[] args)
	{
		MyMath obj = new MyMath();
		//Simple test
		obj.fahrenheit_to_kelvin(1);
		obj.fahrenheit_to_kelvin(14.5);
		obj.fahrenheit_to_kelvin(52.9);
	}
}

Output

Fahrenheit : 1.0 Kelvin : 255.92777777777775
Fahrenheit : 14.5 Kelvin : 263.42777777777775
Fahrenheit : 52.9 Kelvin : 284.76111111111106
/*
  C++ program
  Convert fahrenheit to kelvin
*/
#include<iostream>

using namespace std;
class MyMath
{
	public:
		//Find the kelvin by given fahrenheit
		void fahrenheit_to_kelvin(double fahrenheit)
		{
			// Formula : (fahrenheit - 32) × 5/9 + 273.15 
			// Calculate given fahrenheit to kelvin 
			double kelvin = (fahrenheit - 32) * (5 / 9.0) + 273.15;
			cout << "Fahrenheit : " << fahrenheit << " Kelvin : " << kelvin << "\n";
		}
};
int main()
{
	MyMath obj ;
	//Simple test
	obj.fahrenheit_to_kelvin(1);
	obj.fahrenheit_to_kelvin(14.5);
	obj.fahrenheit_to_kelvin(52.9);
	return 0;
}

Output

Fahrenheit : 1 Kelvin : 255.928
Fahrenheit : 14.5 Kelvin : 263.428
Fahrenheit : 52.9 Kelvin : 284.761
/*
  C# program
  Convert fahrenheit to kelvin
*/
using System;
class MyMath
{
	//Find the kelvin by given fahrenheit
	public void fahrenheit_to_kelvin(double fahrenheit)
	{
		// Formula : (fahrenheit - 32) × 5/9 + 273.15 
		// Calculate given fahrenheit to kelvin 
		double kelvin = (fahrenheit - 32) * (5 / 9.0) + 273.15;
		Console.Write("Fahrenheit : " + fahrenheit + " Kelvin : " + kelvin + "\n");
	}
	public static void Main(String[] args)
	{
		MyMath obj = new MyMath();
		//Simple test
		obj.fahrenheit_to_kelvin(1);
		obj.fahrenheit_to_kelvin(14.5);
		obj.fahrenheit_to_kelvin(52.9);
	}
}

Output

Fahrenheit : 1 Kelvin : 255.927777777778
Fahrenheit : 14.5 Kelvin : 263.427777777778
Fahrenheit : 52.9 Kelvin : 284.761111111111
<?php
/*
  Php program
  Convert fahrenheit to kelvin
*/
class MyMath
{
	//Find the kelvin by given fahrenheit
	public	function fahrenheit_to_kelvin($fahrenheit)
	{
		// Formula : (fahrenheit - 32) × 5/9 + 273.15 
		// Calculate given fahrenheit to kelvin 
		$kelvin = ($fahrenheit - 32) * (5 / 9) + 273.15;
		echo "Fahrenheit : ". $fahrenheit ." Kelvin : ". $kelvin ."\n";
	}
}

function main()
{
	$obj = new MyMath();
	//Simple test
	$obj->fahrenheit_to_kelvin(1);
	$obj->fahrenheit_to_kelvin(14.5);
	$obj->fahrenheit_to_kelvin(52.9);
}
main();

Output

Fahrenheit : 1 Kelvin : 255.92777777778
Fahrenheit : 14.5 Kelvin : 263.42777777778
Fahrenheit : 52.9 Kelvin : 284.76111111111
/*
  Node Js program
  Convert fahrenheit to kelvin
*/
class MyMath
{
	//Find the kelvin by given fahrenheit
	fahrenheit_to_kelvin(fahrenheit)
	{
		// Formula : (fahrenheit - 32) × 5/9 + 273.15 
		// Calculate given fahrenheit to kelvin 
		var kelvin = (fahrenheit - 32) * (5 / 9) + 273.15;
		process.stdout.write("Fahrenheit : " + fahrenheit + " Kelvin : " + kelvin + "\n");
	}
}

function main()
{
	var obj = new MyMath();
	//Simple test
	obj.fahrenheit_to_kelvin(1);
	obj.fahrenheit_to_kelvin(14.5);
	obj.fahrenheit_to_kelvin(52.9);
}
main();

Output

Fahrenheit : 1 Kelvin : 255.92777777777775
Fahrenheit : 14.5 Kelvin : 263.42777777777775
Fahrenheit : 52.9 Kelvin : 284.76111111111106
#   Python 3 program
#   Convert fahrenheit to kelvin

class MyMath :
	# Find the kelvin by given fahrenheit
	def fahrenheit_to_kelvin(self, fahrenheit) :
		#  Formula : (fahrenheit - 32) × 5/9 + 273.15 
		#  Calculate given fahrenheit to kelvin 
		kelvin = (fahrenheit - 32) * (5 / 9) + 273.15
		print("Fahrenheit : ", fahrenheit ," Kelvin : ", kelvin ,"\n", end = "")
	

def main() :
	obj = MyMath()
	# Simple test
	obj.fahrenheit_to_kelvin(1)
	obj.fahrenheit_to_kelvin(14.5)
	obj.fahrenheit_to_kelvin(52.9)

if __name__ == "__main__": main()

Output

Fahrenheit :  1  Kelvin :  255.92777777777775
Fahrenheit :  14.5  Kelvin :  263.42777777777775
Fahrenheit :  52.9  Kelvin :  284.76111111111106
#   Ruby program
#   Convert fahrenheit to kelvin

class MyMath

	# Find the kelvin by given fahrenheit
	def fahrenheit_to_kelvin(fahrenheit)
	
		#  Formula : (fahrenheit - 32) × 5/9 + 273.15 
		#  Calculate given fahrenheit to kelvin 
		kelvin = (fahrenheit - 32) * (5 / 9.0) + 273.15
		print("Fahrenheit : ", fahrenheit ," Kelvin : ", kelvin ,"\n")
	end
end
def main()

	obj = MyMath.new()
	# Simple test
	obj.fahrenheit_to_kelvin(1)
	obj.fahrenheit_to_kelvin(14.5)
	obj.fahrenheit_to_kelvin(52.9)
end
main()

Output

Fahrenheit : 1 Kelvin : 255.92777777777775
Fahrenheit : 14.5 Kelvin : 263.42777777777775
Fahrenheit : 52.9 Kelvin : 284.76111111111106
/*
  Scala program
  Convert fahrenheit to kelvin
*/
class MyMath
{
	//Find the kelvin by given fahrenheit
	def fahrenheit_to_kelvin(fahrenheit: Double): Unit = {
		// Formula : (fahrenheit - 32) × 5/9 + 273.15 
		// Calculate given fahrenheit to kelvin 
		var kelvin: Double = (fahrenheit - 32) * (5 / 9.0) + 273.15;
		print("Fahrenheit : " + fahrenheit + " Kelvin : " + kelvin + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyMath = new MyMath();
		//Simple test
		obj.fahrenheit_to_kelvin(1);
		obj.fahrenheit_to_kelvin(14.5);
		obj.fahrenheit_to_kelvin(52.9);
	}
}

Output

Fahrenheit : 1.0 Kelvin : 255.92777777777775
Fahrenheit : 14.5 Kelvin : 263.42777777777775
Fahrenheit : 52.9 Kelvin : 284.76111111111106
/*
  Swift program
  Convert fahrenheit to kelvin
*/
class MyMath
{
	//Find the kelvin by given fahrenheit
	func fahrenheit_to_kelvin(_ fahrenheit: Double)
	{
		// Formula : (fahrenheit - 32) × 5/9 + 273.15 
		// Calculate given fahrenheit to kelvin 
		let kelvin: Double = (fahrenheit - 32) * (5 / 9) + 273.15;
		print("Fahrenheit : ", fahrenheit ," Kelvin : ", kelvin ,"\n", terminator: "");
	}
}
func main()
{
	let obj: MyMath = MyMath();
	//Simple test
	obj.fahrenheit_to_kelvin(1);
	obj.fahrenheit_to_kelvin(14.5);
	obj.fahrenheit_to_kelvin(52.9);
}
main();

Output

Fahrenheit :  1.0  Kelvin :  255.927777777778
Fahrenheit :  14.5  Kelvin :  263.427777777778
Fahrenheit :  52.9  Kelvin :  284.761111111111

Time Complexity

The time complexity of this program is constant for each conversion, as it involves simple arithmetic operations (subtraction, multiplication, and addition). The number of operations remains the same regardless of the input Fahrenheit 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.

New Comment