Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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 a table by using a function.
Syntax
// Function to convert Fahrenheit to Celsius float fahrenheitToCelsius(float fahrenheit); // Function to convert Celsius to Fahrenheit float celsiusToFahrenheit(float celsius);
Example 1: Fahrenheit to Celsius Conversion Table
Following is the C program for temperature conversion table from Fahrenheit to Celsius −
#include <stdio.h>
float fahrenheitToCelsius(float fh) {
float cl;
cl = (fh - 32) * 5 / 9;
return cl;
}
int main() {
float fh, cl;
int begin = 0, stop = 300;
printf("Fahrenheit \t Celsius<br>");
printf("----------\t-----------<br>");
fh = begin;
while (fh <= stop) {
cl = fahrenheitToCelsius(fh);
printf("%3.0f\t\t%6.1f<br>", fh, cl);
fh = fh + 20;
}
return 0;
}
Fahrenheit Celsius ---------- ----------- 0 -17.8 20 -6.7 40 4.4 60 15.6 80 26.7 100 37.8 120 48.9 140 60.0 160 71.1 180 82.2 200 93.3 220 104.4 240 115.6 260 126.7 280 137.8 300 148.9
Example 2: Celsius to Fahrenheit Conversion Table
Here is the program for converting Celsius to Fahrenheit using a function −
#include <stdio.h>
float celsiusToFahrenheit(float cl) {
float fh;
fh = (cl * 9 / 5) + 32;
return fh;
}
int main() {
float cl, fh;
int begin = 0, stop = 150;
printf("Celsius \t Fahrenheit<br>");
printf("-------\t\t-----------<br>");
cl = begin;
while (cl <= stop) {
fh = celsiusToFahrenheit(cl);
printf("%3.0f\t\t%6.1f<br>", cl, fh);
cl = cl + 10;
}
return 0;
}
Celsius Fahrenheit ------- ----------- 0 32.0 10 50.0 20 68.0 30 86.0 40 104.0 50 122.0 60 140.0 70 158.0 80 176.0 90 194.0 100 212.0 110 230.0 120 248.0 130 266.0 140 284.0 150 302.0
Key Points
- The conversion formulas are: Celsius = (Fahrenheit - 32) × 5/9 and Fahrenheit = (Celsius × 9/5) + 32
- Using functions makes the code modular and reusable for different temperature ranges
- The
whileloop generates the table by incrementing temperature values
Conclusion
Temperature conversion tables can be efficiently created using functions in C. This approach provides clean, readable code and allows easy modification of temperature ranges and increment values.
Advertisements
