C - Conversion from Fahrenheit to Celsius and vice versa
Published: 2022-01-18 13:23:19

Summary: solve a basic exercise of C for the conversion of temperatures, in which the formula of conversion from degrees Fahrenheit to Celsius and the conversion from Celsius to Fahrenheit is implemented.

Table of contents:

The formulas.
Convert Celsius to Fahrenheit in C.
Convert Fahrenheit to Celsius.
Converting temperatures.
Putting it all together.
I know it is a very simple exercise but it is very common when starting to program in ANSI C.

The formulas:
This is a programming exercise, not a math or physics exercise. Therefore I will use the following formulas:

C = (F - 32) / 1.8

F = (C * 1.8) + 32

Convert Celsius to Fahrenheit in C:
I'm going to put the formula in a function and it looks like this:

float  celsiusAFahrenheit ( float celsius) {
    return (celsius * 1 . 8f ) + 32 ;
}
We use the data type floatbecause we are not working with integers. We simply receive the degrees Celsius and return the result of the formula. That is, we multiply by 1.8(the fis to indicate that it is a float) and add it 32.

The value that is returned is floating in the same way.

Convert Fahrenheit to Celsius:
Now let's look at the other formula. It looks like this:

float  fahrenheitACelsius ( float fahrenheit) {
    return (fahrenheit - 32 ) / 1 . 8f ;
}
As you can see, we apply the formula. We subtract 32from the Fahrenheit degrees and divide it by 1.8float. We return the result.

Converting temperatures:
Now that you have declared the functions you can use them. Here is an example of use:

int  main ( void ) {
    printf ( " 30 degrees C to F: % f \ n " , celsiusAFahrenheit ( 30 . 0f ));
    printf ( " 86 degrees F to C: % f \ n " , fahrenheitACelsius ( 86 . 0f ));
}
As you can see, I am printing what the function returns. If you wanted a user to provide the variables it could be something like this using scanf :

float fahrenheit = 0 , celsius = 0 ; // Here we will store the user variables
printf ( " Tell me the degrees F: \ n " );
scanf ( " % f " , & fahrenheit);
float equivalence = fahrenheitACelsius (fahrenheit);
printf ( " Equivalent to % f \ n " , equivalence);
printf ( " Tell me the degrees C: \ n " );
scanf ( " % f " , & celsius);
equivalence = celsiusAFahrenheit (celsius);
printf ( " Equivalent to % f \ n " , equivalence);
Putting it all together:
The complete code is as follows:

# Include  < stdio.h >

float  fahrenheitACelsius ( float fahrenheit) {
    return (fahrenheit - 32 ) / 1 . 8f ;
}

float  celsiusAFahrenheit ( float celsius) {
    return (celsius * 1 . 8f ) + 32 ;
}


int  main ( void ) {
    printf ( " 30 degrees C to F: % f \ n " , celsiusAFahrenheit ( 30 . 0f ));
    printf ( " 86 degrees F to C: % f \ n " , fahrenheitACelsius ( 86 . 0f ));
    float fahrenheit = 0 , celsius = 0 ; // Here we will store the user variables
    printf ( " Tell me the degrees F: \ n " );
    scanf ( " % f " , & fahrenheit);
    float equivalence = fahrenheitACelsius (fahrenheit);
    printf ( " Equivalent to % f \ n " , equivalence);
    printf ( " Tell me the degrees C: \ n " );
    scanf ( " % f " , & celsius);
    equivalence = celsiusAFahrenheit (celsius);
    printf ( " Equivalent to % f \ n " , equivalence);
}
Hope you will be able to understand it!