# Celsius to Fahrenheit C Program

Conversion from Celsius to Fahrenheit and from Fahrenheit to Celsius has an important role in the conversion of units system. The formula used to convert from Celsius to Fahrenheit is given by ⁰F= (⁰C * 9/5) + 32; In this post, we will write Celsius to Fahrenheit conversion C Program.

## Convert Celsius to Fahrenheit using the C Program

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

printf("Enter Celsius value: ");
scanf("%f",&cel);

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

printf("Fahrenheit value = %.2f", fahr);

return 0;
}``````

Output for different test-cases:-

Enter Celsius value: 30
Fahrenheit value = 86.00

Enter Celsius value: 37
Fahrenheit value = 98.60

Enter Celsius value: -40
Fahrenheit value = -40.00

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

Generally, we need to convert Celsius to Fahrenheit in between a range. So, now we will Convert Celsius to Fahrenheit for Celsius = 0,20,40,60,….,200. There are various methods, some of which are given here.

## C Program to Convert Celsius to Fahrenheit In a Range

### Celsius to Fahrenheit 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 cel, fahr ;
int lower, upper, step ;

lower=0, upper=200, step=20 ;
cel = lower ;

while( cel <= upper )
{
fahr= ( cel * (9.0/5.0) ) + 32 ;

printf("%3.0f \t %4.2f \n", cel, fahr ) ;

cel += step ;
}

return 0;
}``````

Output:-

``````   0      32.00
20      68.00
40      104.00
60      140.00
80      176.00
100      212.00
120      248.00
140      284.00
160      320.00
180      356.00
200      392.00 ``````

### Celsius to Fahrenheit C program 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 Celsius to Fahrenheit in a range.

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

for(cel=0 ; cel <= 200 ; cel = cel + 20)
{
printf("%d \t %4.2f \n", cel, ( cel * (9.0/5.0) ) + 32);
}

return 0;
}``````

In the above methods, we initialized directly lower value 0, upper value 200 and step value 20. 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 Celsius to Fahrenheit C Program

``````#include<stdio.h>
#define LOWER 0
#define UPPER 200
#define STEP 20
int main()
{
int cel;

for(cel=LOWER ; cel <= UPPER ; cel = cel + STEP)
{
printf("%d \t %4.2f \n", cel, ( cel * (9.0/5.0) ) + 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!