codescracker
c

C Program to Convert Fahrenheit to Celsius



« Previous Program Next Program »

In this article, you will learn and get code about conversion of temperature value from fahrenheit to celsius. Before going to the program, let's understand about its formula used in conversion.

Fahrenheit to Celsius Formula

The Fahrenheit to Celsius Formula is:

celsius = (fahrenheit-32)*(100/180)

To learn more about this formula, then refer to Celsius to Fahrenheit Formula explanation. There you will get complete understanding about, how this formula derived. I know that the title is for celsius to fahrenheit, but don't care about it, you automatically understand about fahrenheit to celsius formula after reading that article.

The formula given above can also be written as:

celsius = (fahrenheit-32)/(180/100)

And as the value of 180/100 is 1.8, so the previous formula can be further written as:

celsius = (fahrenheit-32)/1.8

Now let's move on to the program written in C for the conversion of fahrenheit to celsius.

Fahrenheit to Celsius in C

The question is, write a program in C that converts temperature given in degree fahrenheit to temperature in degree celsius. The answer to this question is given below:

// Fahrenheit to Celsius in C
// ----codescracker.com-----

#include<stdio.h>
#include<conio.h>
int main()
{
    float fahrenheit, celsius;
    printf("Enter Temperature Value (in Fahrenheit): ");
    scanf("%f", &fahrenheit);
    celsius = (fahrenheit-32)/1.8;
    printf("\nEquivalent Temperature (in Celsius) = %0.2f", celsius);
    getch();
    return 0;
}

This program is compiled and executed in Code::Blocks IDE. Let's have a look at the sample run of above program:

c program convert fahrenheit to centigrade

Now supply any temperature value (in fahrenheit) say 98.6 and press ENTER key to see the following output:

fahrenheit to celsius conversion c

Note - %0.2f format specifier is used to print decimal value upto 2 decimal place.

Fahrenheit to Celsius in C using Function

Let's create another program that does the same job as of previous program. But this time, it is created using user-defined function. This function takes the value of fahrenheit as its parameter and converts it into celsius, then passes it as its return value. To learn more about function, refer to Function in C tutorial.

// Fahrenheit to Celsius in C using Function
// ----codescracker.com-----

#include<stdio.h>
#include<conio.h>
float fahToCelFun(float);
int main()
{
    float fahrenheit, celsius;
    printf("Enter Temperature Value (in Fahrenheit): ");
    scanf("%f", &fahrenheit);
    celsius = fahToCelFun(fahrenheit);
    printf("\nEquivalent Temperature (in Celsius) = %0.2f", celsius);
    getch();
    return 0;
}
float fahToCelFun(float fah)
{
    return ((fah-32)/1.8);
}

The output produced by above program is same as of first program given at early of this article.

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker