Skip to main content

Convert fahrenheit to celsius

Temperature is a critical aspect of daily life and scientific research. Different regions and fields use various temperature scales to measure and express temperature. Fahrenheit (°F) and Celsius (°C) are two widely recognized temperature scales. Celsius is commonly used in most parts of the world, while Fahrenheit is primarily used in the United States for temperature measurements. Converting temperatures between Fahrenheit and Celsius is important for clear communication and accurate calculations.

Problem Statement and Description

The task is to convert a temperature given in Fahrenheit to its equivalent value in Celsius. Celsius is a commonly used temperature scale for everyday temperature measurements. The conversion formula from Fahrenheit to Celsius is celsius = (fahrenheit - 32) × 5/9. This formula subtracts 32 from the Fahrenheit temperature to get the equivalent temperature in Celsius and then multiplies by 5/9 to obtain the temperature in Celsius.

Example

Let's illustrate the problem with an example. Suppose we have a temperature of 68°F that we want to convert to Celsius. Using the formula:

Celsius = (Fahrenheit - 32) × 5/9
    Celsius = (68 - 32) × 5/9
    Celsius ≈ 20

So, 68°F is approximately equivalent to 20°C.

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 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 fahrenheit_to_celsius(fahrenheit):
    celsius = (fahrenheit - 32) × 5/9
    return celsius

main:
    fahrenheit_to_celsius(1)
    fahrenheit_to_celsius(34)
    fahrenheit_to_celsius(89.7)

Algorithm Explanation

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

Program Solution

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

//Find the celsius by given fahrenheit
void fahrenheit_to_celsius(double fahrenheit)
{
	// Formula : (fahrenheit - 32) × 5/9
	// Calculate given fahrenheit to celsius 
	double celsius = (fahrenheit - 32) * (5 / 9.0);
	//Display result
	printf("Fahrenheit : %lf  Celsius : %lf\n", fahrenheit, celsius);
}
int main()
{
	//Simple test
	fahrenheit_to_celsius(1);
	fahrenheit_to_celsius(34);
	fahrenheit_to_celsius(89.7);
	return 0;
}

Output

Fahrenheit : 1.000000  Celsius : -17.222222
Fahrenheit : 34.000000  Celsius : 1.111111
Fahrenheit : 89.700000  Celsius : 32.055556
/*
  Java program
  Convert fahrenheit to celsius
*/
class MyMath
{
	//Find the celsius by given fahrenheit
	public void fahrenheit_to_celsius(double fahrenheit)
	{
		// Formula : (fahrenheit - 32) × 5/9
		// Calculate given fahrenheit to celsius 
		double celsius = (fahrenheit - 32) * (5 / 9.0);
		System.out.print("Fahrenheit : " + fahrenheit + " celsius : " + celsius + "\n");
	}
	public static void main(String[] args)
	{
		MyMath obj = new MyMath();
		//Simple test
		obj.fahrenheit_to_celsius(1);
		obj.fahrenheit_to_celsius(34);
		obj.fahrenheit_to_celsius(89.7);
	}
}

Output

Fahrenheit : 1.0 celsius : -17.22222222222222
Fahrenheit : 34.0 celsius : 1.1111111111111112
Fahrenheit : 89.7 celsius : 32.05555555555556
/*
  C++ program
  Convert fahrenheit to celsius
*/
#include<iostream>

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

Output

Fahrenheit : 1 celsius : -17.2222
Fahrenheit : 34 celsius : 1.11111
Fahrenheit : 89.7 celsius : 32.0556
/*
  C# program
  Convert fahrenheit to celsius
*/
using System;
class MyMath
{
	//Find the celsius by given fahrenheit
	public void fahrenheit_to_celsius(double fahrenheit)
	{
		// Formula : (fahrenheit - 32) × 5/9
		// Calculate given fahrenheit to celsius 
		double celsius = (fahrenheit - 32) * (5 / 9.0);
		Console.Write("Fahrenheit : " + fahrenheit + " celsius : " + celsius + "\n");
	}
	public static void Main(String[] args)
	{
		MyMath obj = new MyMath();
		//Simple test
		obj.fahrenheit_to_celsius(1);
		obj.fahrenheit_to_celsius(34);
		obj.fahrenheit_to_celsius(89.7);
	}
}

Output

Fahrenheit : 1 celsius : -17.2222222222222
Fahrenheit : 34 celsius : 1.11111111111111
Fahrenheit : 89.7 celsius : 32.0555555555556
<?php
/*
  Php program
  Convert fahrenheit to celsius
*/
class MyMath
{
	//Find the celsius by given fahrenheit
	public	function fahrenheit_to_celsius($fahrenheit)
	{
		// Formula : (fahrenheit - 32) × 5/9
		// Calculate given fahrenheit to celsius 
		$celsius = ($fahrenheit - 32) * (5 / 9.0);
		echo "Fahrenheit : ". $fahrenheit ." celsius : ". $celsius ."\n";
	}
}

function main()
{
	$obj = new MyMath();
	//Simple test
	$obj->fahrenheit_to_celsius(1);
	$obj->fahrenheit_to_celsius(34);
	$obj->fahrenheit_to_celsius(89.7);
}
main();

Output

Fahrenheit : 1 celsius : -17.222222222222
Fahrenheit : 34 celsius : 1.1111111111111
Fahrenheit : 89.7 celsius : 32.055555555556
/*
  Node Js program
  Convert fahrenheit to celsius
*/
class MyMath
{
	//Find the celsius by given fahrenheit
	fahrenheit_to_celsius(fahrenheit)
	{
		// Formula : (fahrenheit - 32) × 5/9
		// Calculate given fahrenheit to celsius 
		var celsius = (fahrenheit - 32) * (5 / 9.0);
		process.stdout.write("Fahrenheit : " + fahrenheit + " celsius : " + celsius + "\n");
	}
}

function main()
{
	var obj = new MyMath();
	//Simple test
	obj.fahrenheit_to_celsius(1);
	obj.fahrenheit_to_celsius(34);
	obj.fahrenheit_to_celsius(89.7);
}
main();

Output

Fahrenheit : 1 celsius : -17.22222222222222
Fahrenheit : 34 celsius : 1.1111111111111112
Fahrenheit : 89.7 celsius : 32.05555555555556
#   Python 3 program
#   Convert fahrenheit to celsius

class MyMath :
	# Find the celsius by given fahrenheit
	def fahrenheit_to_celsius(self, fahrenheit) :
		#  Formula : (fahrenheit - 32) × 5/9
		#  Calculate given fahrenheit to celsius 
		celsius = (fahrenheit - 32) * (5 / 9.0)
		print("Fahrenheit : ", fahrenheit ," celsius : ", celsius ,"\n", end = "")
	

def main() :
	obj = MyMath()
	# Simple test
	obj.fahrenheit_to_celsius(1)
	obj.fahrenheit_to_celsius(34)
	obj.fahrenheit_to_celsius(89.7)

if __name__ == "__main__": main()

Output

Fahrenheit :  1  celsius :  -17.22222222222222
Fahrenheit :  34  celsius :  1.1111111111111112
Fahrenheit :  89.7  celsius :  32.05555555555556
#   Ruby program
#   Convert fahrenheit to celsius

class MyMath

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

	obj = MyMath.new()
	# Simple test
	obj.fahrenheit_to_celsius(1)
	obj.fahrenheit_to_celsius(34)
	obj.fahrenheit_to_celsius(89.7)
end
main()

Output

Fahrenheit : 1 celsius : -17.22222222222222
Fahrenheit : 34 celsius : 1.1111111111111112
Fahrenheit : 89.7 celsius : 32.05555555555556
/*
  Scala program
  Convert fahrenheit to celsius
*/
class MyMath
{
	//Find the celsius by given fahrenheit
	def fahrenheit_to_celsius(fahrenheit: Double): Unit = {
		// Formula : (fahrenheit - 32) × 5/9
		// Calculate given fahrenheit to celsius 
		var celsius: Double = (fahrenheit - 32) * (5 / 9.0);
		print("Fahrenheit : " + fahrenheit + " celsius : " + celsius + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyMath = new MyMath();
		//Simple test
		obj.fahrenheit_to_celsius(1);
		obj.fahrenheit_to_celsius(34);
		obj.fahrenheit_to_celsius(89.7);
	}
}

Output

Fahrenheit : 1.0 celsius : -17.22222222222222
Fahrenheit : 34.0 celsius : 1.1111111111111112
Fahrenheit : 89.7 celsius : 32.05555555555556
/*
  Swift program
  Convert fahrenheit to celsius
*/
class MyMath
{
	//Find the celsius by given fahrenheit
	func fahrenheit_to_celsius(_ fahrenheit: Double)
	{
		// Formula : (fahrenheit - 32) × 5/9
		// Calculate given fahrenheit to celsius 
		let celsius: Double = (fahrenheit - 32) * (5 / 9.0);
		print("Fahrenheit : ", fahrenheit ," celsius : ", celsius ,"\n", terminator: "");
	}
}
func main()
{
	let obj: MyMath = MyMath();
	//Simple test
	obj.fahrenheit_to_celsius(1);
	obj.fahrenheit_to_celsius(34);
	obj.fahrenheit_to_celsius(89.7);
}
main();

Output

Fahrenheit :  1.0  celsius :  -17.2222222222222
Fahrenheit :  34.0  celsius :  1.11111111111111
Fahrenheit :  89.7  celsius :  32.0555555555556

Time Complexity

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