facebook like button

25 March, 2011

program 6: Fahrenheit to Celsius conversion

The need:

     In the previous program we saw how we can convert temperature from Fahrenheit to Celsius scale. But if you noticed the output of that program closely, you would have seen that, that program gave the output up-to 6th place of decimal. Suppose I want to control the number of digits in the output after the decimal point then what do I have to do? This program explains that.

The code:
-------------------------------------------------
#include<stdio.h>
int main()
{
    float c,f;            //c and f are float variables
                   // c will be having Celsius and
                    // f will be having Fahrenheit value

    printf("Enter the temperature in degree Fahrenheit.\n");
                            //printing the message for user

    scanf("%f",&f);      //scanning f
    c=5*(f-32)/9;
        /*calculating Celsius value
                          of temperature as you know the
                          formula already*/
    printf("%.2f F = %.2f C\n",f,c);
    return 0;
}
---------------------------------------------------

      Look at the line just above 'return 0;' statement. That is the line used to get the output printed on the 'console' (console is the window in which your program runs.) Usually we just write '%f' to print the float variables but here '%.2f' has been written. When you see the output all the questions about this '%.2f' come to an end. Just see and come back. Yeah you are right.'%.2f' means that we want output up-to 2 places of decimal.

No comments:

Post a Comment

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply