facebook like button

25 March, 2011

program 5: celsius to fahrenheit conversion

The need:

    1. To build a program that will convert any temperature given in degree Celsius to degree Fahrenheit for you. 

    2. To get to know there are not only integers to work with. There are other numbers also like numbers containing a decimal point. I mean to say there exists real numbers other than integers. For handling those numbers there is a built-in 'data-type' float in C. What is a 'data-type' I'll be explaining later in this post.

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 Celsius.\n");
                            //printing the message for user
    scanf("%f",&c);      /*taking input and putting that value in variable c.
                        Now onwards I'll be saying this statement "scanning c"*/
    f=9*c/5 + 32;         //calculating Fahrenheit value of temperature
                         // as you know the formula already
    printf("%f C = %f F\n",c,f);
    return 0;
}
---------------------------------------------------

    Today we come know that there is a 'data-type' float in C and to print and scan float we use '%f' . Now question is what the term 'data-type' means? Here I am not going to tell you in bookish style. In computer perspective every thing is 'data'. I assume you agree with this point. Now if I categorize that data into some parts based on some property, then each part will be having some property or I can say will be of particular type and that type is called the 'data-type'. Like I have already told you int is the data-type for integers and float is a data-type for real numbers including integers.

    Now the question comes "Why is integer data-type existing when float includes integers? Isn't it redundant?"  The answer is : For a processor float arithmetic is more time consuming than that of integers. To save time(to make our program run faster) we use int data-type wherever possible because there are many instances in which there is no need of float (example counter for something). We all know that counters are going to be integers always.

   Next question comes "Is it the only difference between int and float ?" The answer is: "NO".
There is a difference in arithmetic operations also as the following examples shows.

Example 1: i and j are declared to be integers.
int i=7,j=2;
i/j=3;
conclusion: Integer division works as quotient and remainder division and only quotient is returned. 
    To get the remainder we have another operator called the 'modulus' operator. Its symbol is '%'. usage is given below:
int i=7,j=2;
i%j=1;

Example 2: i and j are declared to be float.

float i=7,j=2;
i/j=3.500000;
conclusion: floating point(float) division works as division in which quotient contains decimal point.  So data-type of quotient returned is also float.



No comments:

Post a Comment

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