0
0
CProgramBeginner · 2 min read

C Program to Convert Fahrenheit to Celsius

To convert Fahrenheit to Celsius in C, use the formula celsius = (fahrenheit - 32) * 5 / 9 and print the result; for example, printf("%.2f Celsius", (fahrenheit - 32) * 5.0 / 9.0);.
📋

Examples

Input32
Output0.00 Celsius
Input100
Output37.78 Celsius
Input-40
Output-40.00 Celsius
🧠

How to Think About It

To convert Fahrenheit to Celsius, subtract 32 from the Fahrenheit temperature to remove the offset, then multiply by 5 and divide by 9 to scale the temperature correctly. This formula adjusts the Fahrenheit scale to the Celsius scale.
📐

Algorithm

1
Get the Fahrenheit temperature input from the user
2
Subtract 32 from the Fahrenheit value
3
Multiply the result by 5
4
Divide the product by 9 to get Celsius
5
Print the Celsius temperature
💻

Code

c
#include <stdio.h>

int main() {
    float fahrenheit, celsius;
    printf("Enter temperature in Fahrenheit: ");
    scanf("%f", &fahrenheit);
    celsius = (fahrenheit - 32) * 5.0 / 9.0;
    printf("%.2f Celsius\n", celsius);
    return 0;
}
Output
Enter temperature in Fahrenheit: 100 37.78 Celsius
🔍

Dry Run

Let's trace the input 100 Fahrenheit through the code

1

Input Fahrenheit

User enters 100, so fahrenheit = 100

2

Calculate Celsius

celsius = (100 - 32) * 5.0 / 9.0 = 68 * 5.0 / 9.0 = 340 / 9.0 = 37.7777

3

Print Result

Prints 37.78 Celsius rounded to two decimals

FahrenheitCalculationCelsius
100(100 - 32) * 5 / 937.78
💡

Why This Works

Step 1: Subtract 32

We subtract 32 from Fahrenheit because 32°F equals 0°C, so this removes the offset between scales.

Step 2: Multiply by 5 and divide by 9

Multiplying by 5 and dividing by 9 scales the temperature difference to Celsius units.

Step 3: Print with formatting

Using %.2f in printf formats the output to show two decimal places for clarity.

🔄

Alternative Approaches

Using integer arithmetic
c
#include <stdio.h>

int main() {
    int fahrenheit;
    int celsius;
    printf("Enter temperature in Fahrenheit: ");
    scanf("%d", &fahrenheit);
    celsius = (fahrenheit - 32) * 5 / 9;
    printf("%d Celsius\n", celsius);
    return 0;
}
This uses integer math, so the result is rounded down and less precise.
Using a function for conversion
c
#include <stdio.h>

float toCelsius(float f) {
    return (f - 32) * 5.0 / 9.0;
}

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

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

Only a few variables are used, so the space used is constant.

Which Approach is Fastest?

All approaches run in constant time; using integer arithmetic is slightly faster but less accurate.

ApproachTimeSpaceBest For
Floating-point arithmeticO(1)O(1)Accurate temperature conversion
Integer arithmeticO(1)O(1)Simple, less precise conversion
Function-based conversionO(1)O(1)Reusable and clean code
💡
Always use floating-point numbers to avoid losing decimal precision in temperature conversion.
⚠️
Beginners often forget to use 5.0 and 9.0 as floats, causing integer division and incorrect results.