0
0
CProgramBeginner · 2 min read

C Program to Convert Celsius to Fahrenheit

To convert Celsius to Fahrenheit in C, use the formula fahrenheit = (celsius * 9 / 5) + 32; and print the result with printf.
📋

Examples

Input0
OutputTemperature in Fahrenheit: 32.00
Input25
OutputTemperature in Fahrenheit: 77.00
Input-40
OutputTemperature in Fahrenheit: -40.00
🧠

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. The program should get the Celsius input from the user, apply this formula, and then display the Fahrenheit result.
📐

Algorithm

1
Get the temperature in Celsius from the user.
2
Calculate Fahrenheit using the formula: (Celsius * 9 / 5) + 32.
3
Display the Fahrenheit temperature.
💻

Code

c
#include <stdio.h>

int main() {
    float celsius, fahrenheit;
    printf("Enter temperature in Celsius: ");
    scanf("%f", &celsius);
    fahrenheit = (celsius * 9 / 5) + 32;
    printf("Temperature in Fahrenheit: %.2f\n", fahrenheit);
    return 0;
}
Output
Enter temperature in Celsius: 25 Temperature in Fahrenheit: 77.00
🔍

Dry Run

Let's trace the input 25 through the code

1

Input Celsius

User enters 25, so celsius = 25

2

Calculate Fahrenheit

fahrenheit = (25 * 9 / 5) + 32 = 45 + 32 = 77

3

Print Result

Prints 'Temperature in Fahrenheit: 77.00'

StepCelsiusFahrenheit
Input25-
Calculation2577
Output2577
💡

Why This Works

Step 1: Read Celsius Input

The program uses scanf to get the Celsius temperature from the user.

Step 2: Apply Conversion Formula

It calculates Fahrenheit by multiplying Celsius by 9, dividing by 5, then adding 32 using fahrenheit = (celsius * 9 / 5) + 32;.

Step 3: Display Fahrenheit

Finally, it prints the Fahrenheit temperature with two decimal places using printf.

🔄

Alternative Approaches

Using integer input and output
c
#include <stdio.h>

int main() {
    int celsius, fahrenheit;
    printf("Enter temperature in Celsius: ");
    scanf("%d", &celsius);
    fahrenheit = (celsius * 9 / 5) + 32;
    printf("Temperature in Fahrenheit: %d\n", fahrenheit);
    return 0;
}
This uses integers only, so decimal precision is lost but code is simpler.
Using a function to convert
c
#include <stdio.h>

float toFahrenheit(float celsius) {
    return (celsius * 9 / 5) + 32;
}

int main() {
    float celsius;
    printf("Enter temperature in Celsius: ");
    scanf("%f", &celsius);
    printf("Temperature in Fahrenheit: %.2f\n", toFahrenheit(celsius));
    return 0;
}
This separates conversion logic into a function for better code organization.

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

Time Complexity

The program performs a fixed number of arithmetic operations and input/output calls, so it runs in constant time.

Space Complexity

It uses a few variables for input and output, so space usage is constant.

Which Approach is Fastest?

All approaches run in constant time; using integers is slightly faster but loses precision, while using functions improves readability.

ApproachTimeSpaceBest For
Float calculation inlineO(1)O(1)Precision and simplicity
Integer calculationO(1)O(1)Simple cases without decimals
Function-based conversionO(1)O(1)Code organization and reuse
💡
Always use float or double to keep decimal precision when converting temperatures.
⚠️
Forgetting to add 32 after multiplying and dividing causes wrong Fahrenheit values.