9p / who / tweedy / 9C / 1


Exercise 1.4: Celsius to Fahrenheit Table

Code

#include <u.h>
#include <libc.h>

/* print Celsius-Fahrenheit table for fahr = 0, 20, ..., 300; 
   floating-point version, with header */

void main()
{
    float fahr, celsius;
    int lower, upper, step;

    lower = 0;  /* lower limit of temperature table */
    upper = 300;    /* upper limit */
    step = 20;  /* step size */

    celsius = lower;
    print(" C \t   F\n---\t------\n");
    while (celsius <= upper) {
        fahr = celsius * (9.0/5.0) + 32;
        print("%3.0f\t%6.1f\n", celsius, fahr);
        celsius = celsius + step;
    }
}

Output

$ 9c celstofahr.c; 9l celstofahr.o -o celstofahr
$ ./celstofahr 
 C     F
--- ------
  0   32.0
 20   68.0
 40  104.0
 60  140.0
 80  176.0
100  212.0
120  248.0
140  284.0
160  320.0
180  356.0
200  392.0
220  428.0
240  464.0
260  500.0
280  536.0
300  572.0



tweedy