Computer >> Computer tutorials >  >> Programming >> C programming

How to write the temperature conversion table by using function?


Temperature conversion is nothing but converting Fahrenheit temperature to Celsius or Celsius to Fahrenheit.

In this programming, we are going to explain, how to convert the Fahrenheit temperature to Celsius temperature and how to represent the same in the form of the table by using a function.

Example

Following is the C program for temperature conversion −

#include<stdio.h>
float conversion(float);
int main(){
   float fh,cl;
   int begin=0,stop=300;
   printf("Fahrenheit \t Celsius\n");// display conversion table heading
   printf("----------\t-----------\n");
   fh=begin;
   while(fh<=stop){
      cl=conversion(fh); //calling function
      printf("%3.0f\t\t%6.lf\n",fh,cl);
      fh=fh+20;
   }
   return 0;
}
float conversion(float fh) //called function{
   float cl;
   cl= (fh - 32) * 5 / 9;
   return cl;
}

Output

When the above program is executed, it produces the following result −

Fahrenheit  Celsius
---------- -----------
   0          -18
   20         -7
   40          4
   60          16
   80          27
   100         38
   120         49
   140         60
   160         71
   180         82
   200         93
   220         104
   240         116
   260         127
   280         138
   300         149

In a similar way, you can write the program for converting Celsius to Fahrenheit

By simply changing the equation to

Fahrenheit = (Celsius* 9 / 5) + 32.