Convert celsius to fahrenheit
Temperature conversion is a common task in various fields, from everyday life to scientific research. Different regions use different temperature scales, and converting temperatures between these scales is essential for clear communication and accurate calculations. Celsius (°C) and Fahrenheit (°F) are two commonly used temperature scales. Celsius is commonly used in most countries, while Fahrenheit is primarily used in the United States and a few other countries. Converting temperatures from Celsius to Fahrenheit is a fundamental conversion task.
Problem Statement and Description
The problem is to convert a temperature given in Celsius to its equivalent value in Fahrenheit. The Fahrenheit scale
is based on a mixture of historical conventions and is often used for weather reporting in the United States. The
conversion formula from Celsius to Fahrenheit is (celsius × 9/5) + 32
, where you multiply the Celsius
temperature by 9/5 and then add 32 to get the equivalent temperature in Fahrenheit.
Example
To better understand the problem, let's consider an example. Suppose we have a temperature of 20°C that we want to convert to Fahrenheit. Using the formula:
Fahrenheit = (Celsius × 9/5) + 32
Fahrenheit = (20 × 9/5) + 32
Fahrenheit = 68
So, 20°C is equivalent to 68°F.
Idea to Solve the Problem
To solve this problem, we need to develop a program that takes a temperature in Celsius as input, applies the conversion formula, and outputs the equivalent temperature in Fahrenheit. 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_fahrenheit(celsius):
fahrenheit = (celsius × 9/5) + 32
return fahrenheit
main:
celsius_to_fahrenheit(1)
celsius_to_fahrenheit(10.80)
celsius_to_fahrenheit(21.50)
Algorithm Explanation
- Define the
celsius_to_fahrenheit
function that takes a parametercelsius
. - Inside the function, calculate the equivalent temperature in Fahrenheit using the formula
(celsius × 9/5) + 32
. - Return the calculated temperature in Fahrenheit.
- In the
main
program, call thecelsius_to_fahrenheit
function with different test cases: 1, 10.80, and 21.50. - Print the results.
Program solution
//C Program
//Convert celsius to fahrenheit
#include <stdio.h>
//Find the fahrenheit of given celsius
void celsius_to_fahrenheit(double celsius)
{
// Formula : (celsius × 9/5) + 32
// Calculate given celsius to fahrenheit
double fahrenheit = (celsius * (9 / 5.0)) + 32;
//Display result
printf("Celsius : %lf Fahrenheit : %lf\n", celsius, fahrenheit);
}
int main()
{
//Simple test
celsius_to_fahrenheit(1);
celsius_to_fahrenheit(10.80);
celsius_to_fahrenheit(21.50);
return 0;
}
Output
Celsius : 1.000000 Fahrenheit : 33.800000
Celsius : 10.800000 Fahrenheit : 51.440000
Celsius : 21.500000 Fahrenheit : 70.700000
/*
Java program
Convert the celsius to fahrenheit
*/
class MyMath
{
//Find the fahrenheit of given celsius
public void celsius_to_fahrenheit(double celsius)
{
// Formula : (celsius × 9/5) + 32
// Calculate given celsius to fahrenheit
double fahrenheit = (celsius * (9 / 5.0)) + 32;
System.out.print("Celsius : " + celsius + " Fahrenheit : " + fahrenheit + "\n");
}
public static void main(String[] args)
{
MyMath obj = new MyMath();
//Simple test
obj.celsius_to_fahrenheit(1);
obj.celsius_to_fahrenheit(10.80);
obj.celsius_to_fahrenheit(21.50);
}
}
Output
Celsius : 1.0 Fahrenheit : 33.8
Celsius : 10.8 Fahrenheit : 51.44
Celsius : 21.5 Fahrenheit : 70.7
/*
C++ program
Convert the celsius to fahrenheit
*/
#include<iostream>
using namespace std;
class MyMath
{
public:
//Find the fahrenheit of given celsius
void celsius_to_fahrenheit(double celsius)
{
// Formula : (celsius × 9/5) + 32
// Calculate given celsius to fahrenheit
double fahrenheit = (celsius * (9 / 5.0)) + 32;
cout << "Celsius : " << celsius << " Fahrenheit : " << fahrenheit << "\n";
}
};
int main()
{
MyMath obj ;
//Simple test
obj.celsius_to_fahrenheit(1);
obj.celsius_to_fahrenheit(10.80);
obj.celsius_to_fahrenheit(21.50);
return 0;
}
Output
Celsius : 1 Fahrenheit : 33.8
Celsius : 10.8 Fahrenheit : 51.44
Celsius : 21.5 Fahrenheit : 70.7
/*
C# program
Convert the celsius to fahrenheit
*/
using System;
class MyMath
{
//Find the fahrenheit of given celsius
public void celsius_to_fahrenheit(double celsius)
{
// Formula : (celsius × 9/5) + 32
// Calculate given celsius to fahrenheit
double fahrenheit = (celsius * (9 / 5.0)) + 32;
Console.Write("Celsius : " + celsius + " Fahrenheit : " + fahrenheit + "\n");
}
public static void Main(String[] args)
{
MyMath obj = new MyMath();
//Simple test
obj.celsius_to_fahrenheit(1);
obj.celsius_to_fahrenheit(10.80);
obj.celsius_to_fahrenheit(21.50);
}
}
Output
Celsius : 1 Fahrenheit : 33.8
Celsius : 10.8 Fahrenheit : 51.44
Celsius : 21.5 Fahrenheit : 70.7
<?php
/*
Php program
Convert the celsius to fahrenheit
*/
class MyMath
{
//Find the fahrenheit of given celsius
public function celsius_to_fahrenheit($celsius)
{
// Formula : (celsius × 9/5) + 32
// Calculate given celsius to fahrenheit
$fahrenheit = ($celsius * (9 / 5.0)) + 32;
echo "Celsius : ". $celsius ." Fahrenheit : ". $fahrenheit ."\n";
}
}
function main()
{
$obj = new MyMath();
//Simple test
$obj->celsius_to_fahrenheit(1);
$obj->celsius_to_fahrenheit(10.80);
$obj->celsius_to_fahrenheit(21.50);
}
main();
Output
Celsius : 1 Fahrenheit : 33.8
Celsius : 10.8 Fahrenheit : 51.44
Celsius : 21.5 Fahrenheit : 70.7
/*
Node Js program
Convert the celsius to fahrenheit
*/
class MyMath
{
//Find the fahrenheit of given celsius
celsius_to_fahrenheit(celsius)
{
// Formula : (celsius × 9/5) + 32
// Calculate given celsius to fahrenheit
var fahrenheit = (celsius * (9 / 5.0)) + 32;
process.stdout.write("Celsius : " + celsius + " Fahrenheit : " + fahrenheit + "\n");
}
}
function main()
{
var obj = new MyMath();
//Simple test
obj.celsius_to_fahrenheit(1);
obj.celsius_to_fahrenheit(10.80);
obj.celsius_to_fahrenheit(21.50);
}
main();
Output
Celsius : 1 Fahrenheit : 33.8
Celsius : 10.8 Fahrenheit : 51.44
Celsius : 21.5 Fahrenheit : 70.7
# Python 3 program
# Convert the celsius to fahrenheit
class MyMath :
# Find the fahrenheit of given celsius
def celsius_to_fahrenheit(self, celsius) :
# Formula : (celsius × 9/5) + 32
# Calculate given celsius to fahrenheit
fahrenheit = (celsius * (9 / 5.0)) + 32
print("Celsius : ", celsius ," Fahrenheit : ", fahrenheit ,"\n", end = "")
def main() :
obj = MyMath()
# Simple test
obj.celsius_to_fahrenheit(1)
obj.celsius_to_fahrenheit(10.80)
obj.celsius_to_fahrenheit(21.50)
if __name__ == "__main__": main()
Output
Celsius : 1 Fahrenheit : 33.8
Celsius : 10.8 Fahrenheit : 51.44
Celsius : 21.5 Fahrenheit : 70.7
# Ruby program
# Convert the celsius to fahrenheit
class MyMath
# Find the fahrenheit of given celsius
def celsius_to_fahrenheit(celsius)
# Formula : (celsius × 9/5) + 32
# Calculate given celsius to fahrenheit
fahrenheit = (celsius * (9 / 5.0)) + 32
print("Celsius : ", celsius ," Fahrenheit : ", fahrenheit ,"\n")
end
end
def main()
obj = MyMath.new()
# Simple test
obj.celsius_to_fahrenheit(1)
obj.celsius_to_fahrenheit(10.80)
obj.celsius_to_fahrenheit(21.50)
end
main()
Output
Celsius : 1 Fahrenheit : 33.8
Celsius : 10.8 Fahrenheit : 51.44
Celsius : 21.5 Fahrenheit : 70.7
/*
Scala program
Convert the celsius to fahrenheit
*/
class MyMath
{
//Find the fahrenheit of given celsius
def celsius_to_fahrenheit(celsius: Double): Unit = {
// Formula : (celsius × 9/5) + 32
// Calculate given celsius to fahrenheit
var fahrenheit: Double = (celsius * (9 / 5.0)) + 32;
print("Celsius : " + celsius + " Fahrenheit : " + fahrenheit + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyMath = new MyMath();
//Simple test
obj.celsius_to_fahrenheit(1);
obj.celsius_to_fahrenheit(10.80);
obj.celsius_to_fahrenheit(21.50);
}
}
Output
Celsius : 1.0 Fahrenheit : 33.8
Celsius : 10.8 Fahrenheit : 51.44
Celsius : 21.5 Fahrenheit : 70.7
/*
Swift program
Convert the celsius to fahrenheit
*/
class MyMath
{
//Find the fahrenheit of given celsius
func celsius_to_fahrenheit(_ celsius: Double)
{
// Formula : (celsius × 9/5) + 32
// Calculate given celsius to fahrenheit
let fahrenheit: Double = (celsius * (9 / 5.0)) + 32;
print("Celsius : ", celsius ," Fahrenheit : ", fahrenheit ,"\n", terminator: "");
}
}
func main()
{
let obj: MyMath = MyMath();
//Simple test
obj.celsius_to_fahrenheit(1);
obj.celsius_to_fahrenheit(10.80);
obj.celsius_to_fahrenheit(21.50);
}
main();
Output
Celsius : 1.0 Fahrenheit : 33.8
Celsius : 10.8 Fahrenheit : 51.44
Celsius : 21.5 Fahrenheit : 70.7
Time Complexity
The time complexity of this program is constant for each conversion, as it involves simple arithmetic operations (multiplication and addition). The number of operations remains the same regardless of the input Celsius temperature. Thus, the time complexity is O(1).
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