How to Create a C Program to Convert Fahrenheit to Celsius

Temperature conversion is a common task in programming. If you’re a beginner learning C, writing a program to convert Fahrenheit to Celsius is an excellent way to practice variables, arithmetic operations, and user input. In this guide, we’ll walk you through creating a simple yet effective C program for this conversion.

Understanding the Formula

Before writing the code, let’s understand the mathematical formula for converting Fahrenheit to Celsius:

C=(F−32)×59C = \frac{(F – 32) \times 5}{9}

Where:

  • C = Temperature in Celsius
  • F = Temperature in Fahrenheit

Writing the C Program

Here’s a step-by-step guide to writing a C program for Fahrenheit to Celsius conversion.

1. Include the Necessary Header Files

#include <stdio.h>

The stdio.h header file is required for input and output functions like printf() and scanf().

2. Define the Main Function

int main() {

The main() function is the entry point of the C program.

3. Declare Variables

    float fahrenheit, celsius;

We use the float data type since temperature values can be decimal numbers.

4. Take User Input

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

The printf() function prompts the user to enter the temperature in Fahrenheit, while scanf() reads the input value.

5. Perform the Conversion

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

Using the formula, we convert Fahrenheit to Celsius.

6. Display the Result

    printf("Temperature in Celsius: %.2f\n", celsius);

The printf() function prints the converted temperature up to two decimal places for better readability.

7. Return 0 to Indicate Successful Execution

    return 0;
}

Returning 0 signifies that the program has executed successfully.

Complete C Program

#include <stdio.h>

int main() {
    float fahrenheit, celsius;
    
    // Taking input from user
    printf("Enter temperature in Fahrenheit: ");
    scanf("%f", &fahrenheit);
    
    // Convert Fahrenheit to Celsius
    celsius = (fahrenheit - 32) * 5 / 9;
    
    // Display result
    printf("Temperature in Celsius: %.2f\n", celsius);
    
    return 0;
}

Sample Output

Enter temperature in Fahrenheit: 98.6
Temperature in Celsius: 37.00

Explanation of the Code

  1. The program includes the stdio.h library for input/output operations.
  2. It declares two float variables: fahrenheit and celsius.
  3. It prompts the user to enter the temperature in Fahrenheit and stores it using scanf().
  4. It applies the conversion formula and stores the result in the celsius variable.
  5. It prints the result using printf().

Enhancements

  • Using Functions: You can wrap the conversion logic inside a function for better modularity.
  • Handling Edge Cases: Add validation to ensure user input is a valid number.
  • Loop for Multiple Conversions: Use a loop to allow multiple conversions in one execution.

Example with a Function

#include <stdio.h>

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

int main() {
    float fahrenheit;
    printf("Enter temperature in Fahrenheit: ");
    scanf("%f", &fahrenheit);
    
    printf("Temperature in Celsius: %.2f\n", convertToCelsius(fahrenheit));
    return 0;
}

Conclusion

Writing a C program to convert Fahrenheit to Celsius is a simple yet effective way to strengthen your coding fundamentals. By understanding the formula, taking user input, and displaying the results, you practice essential programming concepts. Try modifying the program by adding error handling or implementing a menu-driven approach for a better learning experience.

Our Placement Guaranteed Course Program, designed by top IITians and senior developers, ensures job placement with a CTC of up to 25 LPA. Get expert guidance, hands-on coding practice, and interview prep tailored for top tech companies like Visa. 👉 Learn More & Secure Your Dream Job!