C Program: Fahrenheit to Celsius

Fahrenheit to Celsius conversion is another classic example present in the book The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie (Chap. 1, 'A Tutorial Introduction').

The conversion of Fahrenheit temperature to Celsius is based on the formula $\text{C}\deg = \frac{5}{9} (\text{F}\deg - 32)$.

We make use of the while loop to print out the Fahrenheit-to-Celsius temperature conversion in a tabular format. The Fahrenheit temperature starts from 0 till 120, incremented to 10 per loop. The \t inside the printf() is the horizontal tab escape sequence.

				
				#include <stdio.h>
				int main() {
					int fahrenheit, celsius, lower, upper;
					unsigned short int increment;
					lower = 0; // lower fixed point
					upper = 120; // upper fixed point
					increment = 10;
					fahrenheit = lower;
					// conversion
					while(fahrenheit <= upper) {
						celsius = 5 * (fahrenheit - 32) / 9;
						printf("%d \t\t %d", fahrenheit, celsius);
						printf("\n"); // next line
						fahrenheit += increment;
					}
					return 0;
				}
				
			

The converted values are printed into the terminal as follows (L: Fahrenheit, R: Celsius).

leonardo fibonacci