C Program to convert a temperature given in Celsius to Fahrenheit

·

Let us write a program to convert a temperature given in Celsius to Fahrenheit - The formula for conversion is
"f = 1.8C + 32"

/* This program converts a Celsius temperature to Fahrenheit */
# include"stdio.h"
main ( )

{
float fahrenheit, celsius;
scanf(“%f”, & celsius);
fahrenhit = 1.8*celsius + 32.0;
printf (“fahrenheit = % f\n”, fahrenheit);

} /* End of main */


Explaination
Note in the above program after # include we have defined the main function. If the state- ment. float fahrenheit, celsius; is observed we see that

This is a declaration which informs the compiler that fahrenheit and celsius are variable names in which floating point number will be stored. By floating point we mean a number with a fractional part.

The statement scanf(“%f”, & celsius) is a command to read a floating point number and store it in variable name celsius. The format string %f indicates that floating point number is to be read. The next statement is an arithmetic statement contents of celsius is multiplied by 1.8 and added to 32.0 and stored in fahrenheit. The number 32 is written with a decimal point to indicate that it is a floating point number. The last statement is to print the answer.

About Me

Blog Archive