# Fahrenheit to Celsius C Program

We have already seen how to convert from Celsius to Fahrenheit, in the same way, we can also convert from Fahrenheit to Celsius. The formula that will be used for this problem is given by `⁰C = (5/9) * (⁰F-32)`. In this post, we will write Fahrenheit to Celsius conversion C Program

``````#include<stdio.h>
int main()
{
float cel, fahr;

printf("Enter Fahrenheit value: ");
scanf("%f", &fahr);

cel = ((5.0/9.0) * (fahr - 32)) ;

printf("Celsius value = %.2f\n", cel);

return 0;
}``````

Output for different test-cases:-

Enter Fahrenheit value: 210
Celsius value = 98.89

Enter Fahrenheit value: 98
Celsius value = 36.67

Enter Fahrenheit value: -40
Celsius value = -40.00

The -40⁰C is equal to the -40⁰F.

Generally, we need to convert Fahrenheit to Celsius in between an interval. So, now, we will Convert to Celsius for Fahrenheit = 0,30,60,90….,500 . There are various methods, some of which are given here.

## C Program to convert Fahrenheit to Celsius in a Range

### Fahrenheit to Celsius C Program using While loop

The while loop can be used to convert celsius to Fahrenheit. While loop in C is a pre-test loop where the expression is evaluated then only statements are executed. It uses a test expression to control the loop. Before every iteration of the loop, the test expression is evaluated.

``````#include<stdio.h>
int main()
{
float celsius, fahr ;
int lower, upper, step ;

lower=0, upper=500, step=30 ;
fahr = lower ;

while ( fahr <= upper )
{
celsius = (5.0/9.0) * (fahr - 32) ;

printf("%3.0f \t %6.1f \n", fahr , celsius) ;

fahr += step ;  // fahr = fahr + step
}

return 0;
}``````

Output:-

``````   0       -17.8
30        -1.1
60        15.6
90        32.2
120        48.9
150        65.6
180        82.2
210        98.9
240       115.6
270       132.2
300       148.9
330       165.6
360       182.2
390       198.9
420       215.6
450       232.2
480       248.9 ``````

### Fahrenheit to Celsius using For loop

The for loop in C programming language is a pre-test loop, where first of all initialization expression is evaluated then the condition is checked and if the condition is true then only the statements of the for loop are executed. We can use for loop to convert Fahrenheit to Celsius in a range.

``````#include<stdio.h>
int main()
{
int fahr ;

for(fahr=0 ; fahr <= 500 ; fahr = fahr + 30)
{
printf("%d \t %6.2f \n", fahr, (5.0/9.0) * (fahr - 32) );
}

return 0;
}``````

In the above methods, we initialized directly lower value 0, upper value 500 and step value 30. For simplicity and good programming practice, we will don’t initialized these values directly within the main() function. In Symbolic constants, names are conventionally written in uppercase so they can be readily distinguished from lower case variable names. Below is another way to write Fahrenheit to Celsius C Program

``````#include<stdio.h>
#define LOWER 0
#define UPPER 500
#define STEP 30
int main()
{
int fahr ;

for(fahr=LOWER ; fahr <= UPPER ; fahr = fahr + STEP )
{
printf("%d \t %6.2f \n", fahr, (5.0/9.0) * (fahr - 32) );
}

return 0;
}``````

Notice that there is no semicolon at the end of a #define line.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!