facebook like button

25 March, 2011

program 8: temperature conversion (C to F and vice versa)

The need:
     Here this is a program to do temperature conversion based on the user's wish . Here is a program which will ask the user to choose whether he wants to convert temperature from degree C to degree F or vice-versa.

The Code:
--------------------------------------------------------
#include<stdio.h>
main()
{
    float c,f;
    int x;
    printf("for C to F conversion press 1 and for F to C press any other key\n");
    scanf("%d",&x);      //scanning x
    if (x==1)
    {
      printf("Enter the value of temp in degree C\n");
      scanf("%f",&c);
      f=9*c/5 + 32;     // formula for C to F
      printf("%f C = %f F\n",c,f);
    }
    else
    {
      printf("Enter the value of temp in degree F\n");
      scanf("%f",&f);
      c=5*(f-32)/9;     // formula for F to C
      printf("%.2f F = %f C\n",f,c);
    }
}
--------------------------------------------------------

    The new thing about this program is that the whole block of code that lies inside the braces after if statement is getting executed and not just the single line that follows 'if'(As in the previous post). It is the same case with else. So today we have also come to know that if we want the whole block of code to run as a set in a "loop" or 'after a condition'('if' or 'else')we should enclose that block in a pair of braces. Here you have seen a new term 'loop'. I'll be explaining about this in upcoming posts.

No comments:

Post a Comment

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