0
0
CppProgramBeginner · 2 min read

C++ Program to Convert Celsius to Fahrenheit

To convert Celsius to Fahrenheit in C++, use the formula fahrenheit = (celsius * 9.0 / 5.0) + 32. For example, double fahrenheit = (celsius * 9.0 / 5.0) + 32; converts a Celsius value to Fahrenheit.
📋

Examples

Input0
OutputTemperature in Fahrenheit: 32
Input25
OutputTemperature in Fahrenheit: 77
Input-40
OutputTemperature in Fahrenheit: -40
🧠

How to Think About It

To convert Celsius to Fahrenheit, multiply the Celsius temperature by 9, divide by 5, then add 32. This formula changes the scale from Celsius to Fahrenheit, which have different zero points and increments.
📐

Algorithm

1
Get the temperature value in Celsius from the user.
2
Multiply the Celsius value by 9.
3
Divide the result by 5.
4
Add 32 to the result to get Fahrenheit.
5
Display the Fahrenheit temperature.
💻

Code

cpp
#include <iostream>
using namespace std;

int main() {
    double celsius, fahrenheit;
    cout << "Enter temperature in Celsius: ";
    cin >> celsius;
    fahrenheit = (celsius * 9.0 / 5.0) + 32;
    cout << "Temperature in Fahrenheit: " << fahrenheit << endl;
    return 0;
}
Output
Enter temperature in Celsius: 25 Temperature in Fahrenheit: 77
🔍

Dry Run

Let's trace the input 25 Celsius through the code.

1

Input Celsius

User enters 25 for celsius.

2

Calculate Fahrenheit

fahrenheit = (25 * 9.0 / 5.0) + 32 = (225 / 5) + 32 = 45 + 32 = 77

3

Output Result

Prints 'Temperature in Fahrenheit: 77'

VariableValue
celsius25
fahrenheit77
💡

Why This Works

Step 1: Multiply Celsius by 9

Multiplying by 9 scales the Celsius temperature to match the Fahrenheit scale's ratio.

Step 2: Divide by 5

Dividing by 5 adjusts the scale difference between Celsius and Fahrenheit degrees.

Step 3: Add 32

Adding 32 shifts the zero point from Celsius to Fahrenheit, since 0°C equals 32°F.

🔄

Alternative Approaches

Using a function
cpp
#include <iostream>
using namespace std;

double toFahrenheit(double celsius) {
    return (celsius * 9.0 / 5.0) + 32;
}

int main() {
    double celsius;
    cout << "Enter temperature in Celsius: ";
    cin >> celsius;
    cout << "Temperature in Fahrenheit: " << toFahrenheit(celsius) << endl;
    return 0;
}
This approach improves code reuse by separating conversion logic into a function.
Using integer arithmetic (less precise)
cpp
#include <iostream>
using namespace std;

int main() {
    int celsius, fahrenheit;
    cout << "Enter temperature in Celsius: ";
    cin >> celsius;
    fahrenheit = (celsius * 9 / 5) + 32;
    cout << "Temperature in Fahrenheit: " << fahrenheit << endl;
    return 0;
}
This uses integers only but loses decimal precision, suitable for whole number temperatures.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of arithmetic operations regardless of input size, so it runs in constant time O(1).

Space Complexity

Only a few variables are used to store input and output, so space complexity is constant O(1).

Which Approach is Fastest?

All approaches run in constant time; using a function adds clarity but no significant overhead.

ApproachTimeSpaceBest For
Direct calculationO(1)O(1)Simple quick conversion
Function-basedO(1)O(1)Reusable code and clarity
Integer arithmeticO(1)O(1)Whole number temperatures, less precision
💡
Use double type for temperature to handle decimal values accurately.
⚠️
Forgetting to use 9.0 or 5.0 as floating-point numbers causes integer division and wrong results.