summaryrefslogtreecommitdiff
path: root/ch1/ftoc.c
blob: 70b994ae739659d057288f9eedbebd720c04739d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<stdio.h>

#define     LOWER   0       // lower limit of table 
#define     UPPER   300     // upper limit of table 
#define     STEP    20      // step size

float ftoc(int fahr);

/*
 * Print Fahrenheit-Celcius table for fahr = 0, 20, ... 300.
 */
main() {
    float fahr, celcius;
    int lower, upper, step;

    lower = LOWER;      // lower limit of temperature table
    upper = UPPER;    // upper limit of temperature table
    step = STEP;      // step size

    fahr = lower;

    printf("%3s %6s\n", "F", "C");

    //    while (fahr <= upper) {
    //        celcius = 5.0 / 9.0 * (fahr - 32.0);
    //        printf("%3.0f %6.1f\n", fahr, celcius);
    //        fahr = fahr + step;
    //    }

    for (fahr = UPPER; fahr >= LOWER; fahr = fahr - STEP) {
        printf("%3.0f %6.1f\n", fahr, ftoc(fahr));
    }
}

float ftoc(int fahr) {
    return (5.0 / 9.0) * (fahr - 32);
}