Exercise 1-3 (Temperature heading)
Chapter_1 Exercise_1-2 Truncate | Exercise_1-4 |
Exercise 1-3 K&R, p. 13
Exercise 1-3. Modify the temperature conversion program to print a heading above the table.
tempfh.c download
#include <stdio.h> // for printf()
/*
print Fahrenheit-Celsius table
for fahr = 0, 20, ..., 300
(floating-point version)
*/
int main()
{
float fahr, celsius; // main vars used for output
int lower, upper, step; // vars used for computation
lower = 0; // lower limit of temperature table
upper = 300; // upper limit
step = 20; // step size
// Heading:
printf("Fahrenheit-Celsius\n");
printf("conversion table\n");
printf("------------------------\n");
fahr = lower;
while(fahr <= upper)
{
celsius = (5.0 / 9.0) * (fahr-32.0);
printf("%3.0f %6.1f\n", fahr, celsius);
fahr += step; // fahr = fahr + step;
}
}
/*
gcc tempfh.c -o tempfh
./tempfh
Fahrenheit-Celsius
conversion table
------------------------
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
*/
Chapter_1 Exercise_1-2 Truncate | BACK_TO_TOP | Exercise_1-4 |
Comments
Post a Comment