🚀Beginner

Calculate Fahrenheit to Celsius

Temperature conversion program

This program converts temperature from Fahrenheit to Celsius by applying the conversion formula. For example, 82°F is equal to 27.7°C.

📐 Formula to Convert Fahrenheit to Celsius

T(°C) = (T(°F) - 32) × 5/9

T(°C): Temperature in Celsius

T(°F): Temperature in Fahrenheit

📋 Algorithm

  1. Define temperature in Fahrenheit units
  2. Apply the formula to convert Fahrenheit to Celsius
  3. Print the temperature in Celsius

Program to Convert Fahrenheit to Celsius

fahrenheit_to_celsius.c
C
1// C Program to convert
2// Fahrenheit to Celsius
3#include <stdio.h>
4
5// Function to convert Degree
6// Fahrenheit to Degree Celsius
7float fahrenheit_to_celsius(float f)
8{
9 return ((f - 32.0) * 5.0 / 9.0);
10}
11
12// Driver code
13int main()
14{
15 float f = 40;
16
17 // Passing parameter to function
18 printf("Temperature in Degree Celsius : %0.2f",
19 fahrenheit_to_celsius(f));
20 return 0;
21}
Output

Temperature in Degree Celsius : 4.44

Program with User Input

fahrenheit_celsius_input.c
C
1#include <stdio.h>
2
3int main()
4{
5 float fahrenheit, celsius;
6
7 // Get temperature in Fahrenheit from user
8 printf("Enter temperature in Fahrenheit: ");
9 scanf("%f", &fahrenheit);
10
11 // Convert to Celsius using formula
12 celsius = (fahrenheit - 32) * 5.0 / 9.0;
13
14 // Display the result
15 printf("%.2f°F = %.2f°C\n", fahrenheit, celsius);
16
17 return 0;
18}
$ ./fahrenheit_celsius

Enter temperature in Fahrenheit: 98.6\n98.60°F = 37.00°C

📖 Code Explanation

CodeExplanation
float f = 40;Declare and initialize Fahrenheit temperature
(f - 32.0)Subtract 32 from Fahrenheit value
* 5.0 / 9.0Multiply by 5/9 to get Celsius
%0.2fFormat specifier for 2 decimal places
5.0 / 9.0Use float division (not 5/9 which gives 0)

⚠️ Why use 5.0/9.0 instead of 5/9?

In C, 5/9 performs integer division and returns 0! Using 5.0/9.0 ensures floating-point division which gives the correct result 0.5556.

🔄 Step-by-Step Calculation

Converting 40°F to Celsius:

Step 1: Subtract 32 → 40 - 32 = 8

Step 2: Multiply by 5 → 8 × 5 = 40

Step 3: Divide by 9 → 40 ÷ 9 = 4.44°C

📊 Common Temperature Conversions

FahrenheitCelsiusSignificance
32°F0°CFreezing point of water
68°F20°CRoom temperature
98.6°F37°CHuman body temperature
212°F100°CBoiling point of water

⚡ Complexity Analysis

Time Complexity

O(1)

Space Complexity

O(1)

🎯 Key Takeaways

Formula: °C = (°F - 32) × 5/9
Use float division (5.0/9.0)
32°F = 0°C (Freezing point)
212°F = 100°C (Boiling point)

Want to Learn More?

Explore our comprehensive tutorials for in-depth explanations of C programming concepts.

Browse Tutorials
Back to All Examples