Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
C++ program to convert Fahrenheit to Celsius
To convert fahrenheit to celsius, use a simple mathematical formula. In this article, we are going to discuss the conversion formula, a simple example, and an easy example code.
We have been given a temperature in fahrenheit and our task is to convert the given temperature into celsius using C++.
Understanding Fahrenheit to Celsius Formula
The formula for converting the given temperature from fahrenheit to celsius is as follows:
celsius = (fahrenheit - 32.0) * 5.0 / 9.0;
Let's see an example to convert the temperature given in fahrenheit to celsius using the above formula:
Input: Temperature in Fahrenheit: 122.0deg Solution: celsius = (fahrenheit - 32.0) * 5.0 / 9.0; celsius = (122.0 - 32.0) * 5.0 / 9.0 = 50deg Output: Temperature in Celsius: 50deg
Program to Convert Fahrenheit to Celsius
The following program demonstrates the conversion of temperature given in fahrenheit to celsius:
#include<iostream>
using namespace std;
int main() {
float fahrenheit = 100, celsius;
cout << "The tempertaure in Fahrenheit is: " << fahrenheit << endl;
celsius = (fahrenheit - 32) * 5 / 9;
cout << "Equivalent Celsius temperature is: " << celsius << endl;
}
Fahrenheit to Celsius Conversion Using Custom Function
In this example, we have used a custom function toCelsius() and passed fahrenheit as an argument. The function returns the celsius value after conversion.
#include<iostream>
using namespace std;
float toCelsius(float fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}
int main() {
float fahrenheit = 100.0;
float celsius = toCelsius(fahrenheit);
cout << "Temperature in Fahrenheit: " << fahrenheit << endl;
cout << "Equivalent Celsius temperature is: " << celsius << endl;
}
