C Program to Convert Fahrenheit Temperature to Celsius


When I just started coding, this was the program which made me to think, I din’t satisfied with the lengthy and complicated one program which I made first, than I went step by step to make it short and more efficient. Simply I use the rule to use less variables as possible as can to make program size short and run faster. I look for the program to eliminate the more variables as can possible. And finally I came up with the short and tricky one with proper format of the output.

Look at the Program Carefully ►►

#include<stdio.h>

#include<conio.h>

void main()

{

int fahr;

for(fahr=0;fahr<=300;fahr+=20)

printf(“%3d  %6.2f \n”,fahr,(9.0/5.0)*(fahr-32));

getch();

}

Thats it !! I just wondered when I use to write the lengthy one. 

Here I only use fahr variable which I use in the for loop as a Fahrenheit number. I condition for loop (fahr<300) to calculate up to 300 and use a compound operator(+=) which means same as fahr=fahr+20; Then I printed out the output directly using printf because I don’t need the Celsius result anywhere else in the program, so why to store.

Did you notice the format specifiers(%3d and %6.2f). The number in between the specifier %3d is to print the integer value 3 characters wide and float value 6 characters wide. The number after the decimal point in the %6.2f will print out the output after the decimal point 2 characters wide. Using this you will get an impressive and great output of the program.

And Ya the parenthesis in the expression is very important because the priority of the * and / operator is greater than the – or + operator. If you don;t use these then the divide and multiply will occur first and the minus will after that may yield an unpredictable result. In C the parenthesis () have the first priority means the expression in the parenthesis will evaluate first.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s