C Program to Convert Fahrenheit to Celsius

C Program to Convert Fahrenheit to Celsius

Temperature conversion is a common task in programming and real-world applications. While converting Celsius to Fahrenheit is often taught first, converting Fahrenheit to Celsius is equally important. This program teaches you how to handle user input, perform arithmetic calculations, and display output using C. By writing a complete program for this conversion, you will understand the use of variables, mathematical expressions, and formatted output in C.

The formula to convert Fahrenheit to Celsius is:

Celsius = (Fahrenheit – 32) × 5 / 9

This formula gives you the Celsius value for any temperature entered in Fahrenheit. Implementing this conversion in C helps beginners practice input/output operations, arithmetic operations, and program structure.

C Program to Convert Fahrenheit to Celsius

Here is a simple program that reads a temperature in Fahrenheit from the user and converts it to Celsius.

#include <stdio.h>

int main() {

    float fahrenheit, celsius;

    printf("Enter temperature in Fahrenheit: ");
    scanf("%f", &fahrenheit);

    celsius = (fahrenheit - 32) * 5 / 9;

    printf("%.2f Fahrenheit = %.2f Celsius\n", fahrenheit, celsius);

    return 0;

}

In this program, we declare two float variables: fahrenheit and celsius. Using printf(), we prompt the user to enter the temperature in Fahrenheit. The scanf() function reads the user input and stores it in fahrenheit. We then calculate the Celsius value using the formula (fahrenheit - 32) * 5 / 9 and store it in celsius. Finally, we display both the original Fahrenheit value and the converted Celsius value with two decimal places for clarity.

Using a Function for Conversion

To make the program modular and reusable, we can define a function that converts Fahrenheit to Celsius. This approach is useful when the conversion needs to be performed multiple times.

#include <stdio.h>

float fahrenheitToCelsius(float f) {
    return (f - 32) * 5 / 9;
}

int main() {

    float fahrenheit;

    printf("Enter temperature in Fahrenheit: ");
    scanf("%f", &fahrenheit);

    float celsius = fahrenheitToCelsius(fahrenheit);

    printf("%.2f Fahrenheit = %.2f Celsius\n", fahrenheit, celsius);

    return 0;

}

In this version, we define a function fahrenheitToCelsius() that takes a float as input and returns the corresponding Celsius value. Inside main(), we call this function with the user-provided Fahrenheit value. Using a function makes the program cleaner, easier to read, and demonstrates the concept of modular programming in C.

Common Beginner Mistakes

When converting temperatures in C, beginners often make these mistakes:

  • Using int variables instead of float or double. Integer division truncates decimals, which can produce inaccurate results.
  • Forgetting parentheses around (fahrenheit - 32) in the formula. Without parentheses, subtraction may not happen first, leading to incorrect calculations.

To avoid these issues, always use the correct data type for decimal numbers and follow the proper order of operations in formulas.

FAQs

Q1: Can this program handle negative Fahrenheit values?
Yes, the program works for both positive and negative Fahrenheit values. For example, -40°F correctly converts to -40°C, which is the unique point where Celsius equals Fahrenheit.

Q2: How can I round the Celsius value to the nearest integer?
You can include the math.h library and use the round() function: celsius = round((fahrenheit - 32) * 5 / 9);

Q3: Can this program be extended to handle Celsius to Fahrenheit conversion as well?
Yes, you can add another function for Celsius to Fahrenheit conversion and allow the user to select the type of conversion.

Conclusion

Converting Fahrenheit to Celsius is a fundamental exercise in C programming. It helps you practice taking input, performing arithmetic calculations, using functions, and displaying formatted output. By implementing this program, you gain confidence in using variables, formulas, and modular programming. Try modifying the program to handle multiple temperature conversions or to allow the user to choose conversion direction to further improve your skills.

References & Additional Resources

  1. Kernighan, Brian W., and Dennis M. Ritchie. The C Programming Language. 2nd Edition, Prentice Hall, 1988.
  2. GeeksforGeeks: C Program To Convert Fahrenheit To Celsius – Examples and explanations.
  3. Programiz: C Functions – Learn how to use and create functions in C.
  4. TutorialsPoint: Input and Output Functions in C – Basics of scanf() and printf().
Scroll to Top