C program to convert fahrenheit to celsius

In this blog post, we learn how to write a C program to convert Fahrenheit to Celsius?. We will write the C program to convert Fahrenheit to Celsius. Write a C program to input temperature in Fahrenheit and convert it to Centigrade. How to convert temperature from degree Fahrenheit to degree centigrade in C programming. Logic to convert temperature from Fahrenheit to Celsius in C.

Example,

Input : 32
Output : 0


Input :- 40
Output : -40

 

Formula to convert Fahrenheit to Celsius:

Below is a formula for how you can convert Fahrenheit to Celsius in your C Code.

Fahrenheit to Celsius: (°F − 32) x 5/9 = °C

 

C program to convert Fahrenheit to Celsius:

The below program asks the user to enter the temperature in Fahrenheit. After getting the temperature in Fahrenheit from the user program convert it into Celsius. So in simple words, we are following the below steps to convert temperature Celsius to Fahrenheit:

  • Get the user-given temperature in Fahrenheit units.
  • Apply in the formula Celsius = (°F − 32) x 5/9.
  • Now print the temperature in Celsius.
#include <stdio.h>

int main()
{
    float celsius, fahrenheit;

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

    //celsius to fahrenheit conversion formula
    celsius = (fahrenheit - 32) / 1.8;

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

    return 0;
}

Output:

Enter temperature in Fahrenheit: 32
32.00 Celsius = 0.00 Fahrenheit

 

C program to convert Fahrenheit to Celsius using a function:

The below program asks the user to enter the temperature in Celsius. After getting the temperature in Celsius; call a function named convertCelFahrenheit() to convert the temperature from Celsius to Fahrenheit.

#include <stdio.h>

float convertFahCelsius(float fh)
{
    return ((fh - 32) / 1.8);
}

int main()
{
    float celsius, fahrenheit;

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

    //called function to convert fahrenheit to celsius
    celsius = convertFahCelsius(fahrenheit);

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

    return 0;
}

Output:

Enter temperature in Fahrenheit: 100
100.00 Fahrenheit = 37.78 Celsius

 

 

Write a generic C program which converts Celsius to Fahrenheit and vice versa:

We already know the formula to convert Celsius to Fahrenheit and Fahrenheit to Celsius. So let’s see a C program that asks the user to choose and converts the temperature unit accordingly.

#include <stdio.h>

int main()
{
    float fh,cl;
    char ch;

    printf("\n\n Press c to convert temperature from Fahrenheit to Celsius.");
    printf("\n\n Press f to convert temperature from Celsius to Fahrenheit.");
    printf("\n\n Enter your choice (c, f): ");
    scanf("%c",&ch);

    if((ch =='c') ||(ch =='C'))
    {
        printf("\n\nEnter temperature in Fahrenheit: ");
        scanf("%f",&fh);
        cl= (fh - 32) / 1.8;
        printf("\n\nTemperature in Celsius: %.2f\n\n",cl);
    }
    else if((ch =='f') ||(ch =='F'))
    {
        printf("\n\nEnter temperature in Celsius: ");
        scanf("%f",&cl);
        fh= (cl*1.8)+32;
        printf("\n\nTemperature in Fahrenheit: %.2f\n\n",fh);
    }
    else
    {
        printf("\n\nInvalid Choice !!!\n\n");
    }

    return 0;
}

Output:

c if else

 

Recommended Post: