C Example Code2026-03-252 min read
Build a temperature conversion program in C to convert between Celsius, Fahrenheit, and Kelvin using precise floating-point formulas.
Conversion Formulas
Converting between international temperature scales:
- Celsius to Fahrenheit:
F = (C × 9/5) + 32 - Fahrenheit to Celsius:
C = (F - 32) × 5/9 - Celsius to Kelvin:
K = C + 273.15
Complete C Implementation
c (ISO Standard)#include <stdio.h> double celsiusToFahrenheit(double c) { return (c * 9.0 / 5.0) + 32.0; } double fahrenheitToCelsius(double f) { return (f - 32.0) * 5.0 / 9.0; } double celsiusToKelvin(double c) { return c + 273.15; } int main() { double celsiusValues[] = {0.0, 25.0, 37.0, 100.0, -40.0}; int count = sizeof(celsiusValues) / sizeof(celsiusValues[0]); printf("%-12s %-15s %-12s\n", "Celsius (°C)", "Fahrenheit (°F)", "Kelvin (K)"); printf("---------------------------------------------\n"); for (int i = 0; i < count; i++) { double c = celsiusValues[i]; double f = celsiusToFahrenheit(c); double k = celsiusToKelvin(c); printf("%-12.2f %-15.2f %-12.2f\n", c, f, k); } return 0; }
Sample Output
text (ISO Standard)Celsius (°C) Fahrenheit (°F) Kelvin (K) --------------------------------------------- 0.00 32.00 273.15 25.00 77.00 298.15 37.00 98.60 310.15 100.00 212.00 373.15 -40.00 -40.00 233.15
Complexity Analysis
- Time Complexity:
O(1)direct arithmetic calculation. - Space Complexity:
O(1)zero memory allocation overhead.
Related Programs
- Practice arithmetic logic in Simple Calculator in C.
- Review fundamental types in Data Types in C.