codescracker
c++

C++ Program to Convert Fahrenheit to Celsius



« Previous Program Next Program »


In this article, you will learn and get code on temperature conversion, that is the conversion of a temperature from Fahrenheit to Celsius (or Centigrade) with or without using Function in C++.

Fahrenheit to Celsius Formula

To convert temperature given in Fahrenheit to its equivalent value in Celsius, use following formula:

C = (F-32)*(100/180)

Where C and F indicates to the value of Fahrenheit and Celsius. This formula can also be written as:

C = (F-32)/(180/100)

Or,

C = (F-32)/1.8

If you're curious to know about the concept behind the formula, then refer to Celsius to Fahrenheit Formula Explained.

Fahrenheit to Celsius in C++

To convert temperature from Fahrenheit to Celsius in C++ programming, you have to ask from user to enter the temperature in Fahrenheit first. And then convert it into its equivalent value in Celsius with the formula given above.

The question is, write a program in C++ that receives temperature in degree Fahrenheit and prints its equivalent value in degree Celsius.. Here is its answer:

// C++ Program to Convert Fahrenheit to Celsius
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    float fahrenheit, celsius;
    cout<<"Enter the Temperature in Fahrenheit: ";
    cin>>fahrenheit;
    celsius = (fahrenheit-32)/1.8;
    cout<<"\nEquivalent Temperature in Celsius: "<<celsius;
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program convert temperature Fahrenheit to Celsius

Now supply the temperature input in Fahrenheit say 80 and press ENTER key to convert and prints its equivalent value in Celsius as shown in the snapshot given below:

fahrenheit to celsius c++

Using user-defined Function

Let's create the same purpose program using a user-defined function FahrenheitToCelsius(). This function takes fahrenheit value as its argument and returns its equivalent Celsius value. Its return value gets initialized to celsius inside the main() function. So print the value of celsius

// Fahrenheit to Celsius using user-defined Function
// ----codescracker.com----

#include<iostream>
using namespace std;
float FahrenheitToCelsius(float);
int main()
{
    float fahrenheit, celsius;
    cout<<"Enter the Temperature in Fahrenheit: ";
    cin>>fahrenheit;
    celsius = FahrenheitToCelsius(fahrenheit);
    cout<<endl<<fahrenheit<<"\370F = "<<celsius<<"\370C";
    cout<<endl;
    return 0;
}
float FahrenheitToCelsius(float f)
{
    float c;
    c = (f-32)/1.8;
    return c;
}

Here is its sample run with user input as 98:

fahrenheit to celsius using function c++

Note - The \370 is used to print degree (o).

Same Program in Other Languages


« Previous Program Next Program »