K&R演習[1-3]

課題

表の上に見出しを印字するように温度換算プログラムを変更せよ。

方針

見出しを印字する。

ソースコード

#include <stdio.h>

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

    lower = 0;      /* 温度表の下限 */
    upper = 300;    /* 温度表の上限 */
    step = 20;      /* きざみ */

    fahr = lower;   /* 温度の初期値 */
    printf("Conversion (Fahrenheit -> Celsius)\n");
    while (fahr <= upper) {
        celsius = (5.0/9.0) * (fahr - 32.0);
        printf("%3.0f %6.1f\t\n", fahr, celsius);
        fahr = fahr + step;
    }
}

実行結果

Conversion (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