codescracker
c++

C++ Program to Convert Celsius to Fahrenheit



« Previous Program Next Program »


In this article, you will learn and get code on Celsius to Fahrenheit temperature conversion in C++. The program is created with and without using function.

Celsius to Fahrenheit Formula

To convert any temperature from degree Celsius to degree Fahrenheit, use the following formula:

F = (C * 1.8) + 32

where F indicates the value of Fahrenheit and C indicates the value of Celsius.

If you're curious to know, from where this formula came, you can refer to Celsius to Fahrenheit formula explained.

Centigrade to Fahrenheit in C++

To convert temperature from Celsius (or centigrade) to Fahrenheit in C++ programming, you have to ask from user to enter the temperature in Celsius. And then convert it into its equivalent value in Fahrenheit using the formula given above, as shown in the program given below:

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

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

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

C++ program convert temperature Celsius to Fahrenheit

Now supply the temperature input in degree Celsius say 37 and press ENTER key to print its equivalent value in degree Fahrenheit as shown here in the following output:

celsius to fahrenheit c++

Using user-defined Function

This program uses a user defined function CelsiusToFahrehneit(), that converts any temperature from celsius to fahrenheit. This function takes celsius value as its argument and returns its equivalent value in fahrenheit.

// Celsius to Fahrenheit Conversion in C++ using Function
// ----codescracker.com----

#include<iostream>
using namespace std;
float CelsiusToFahrehneit(float);
int main()
{
    float celsius, fahrenheit;
    cout<<"Enter the Temperature in Celsius: ";
    cin>>celsius;
    fahrenheit = CelsiusToFahrehneit(celsius);
    cout<<endl<<celsius<<"\370C = "<<fahrenheit<<"\370F";
    cout<<endl;
    return 0;
}
float CelsiusToFahrehneit(float celsius)
{
    return ((celsius*1.8)+32);
}

Here is its sample run with user input 37:

celsius to fahrenheit using function c++

Note - 370 is the octal index of ASCII code of degree symbol (o). Therefore, \370 is used to print degree symbol on output.

Note - C and F are the symbols used for Celsius and Fahrenheit.

Same Program in Other Languages


« Previous Program Next Program »