#include <stdio.h> // for printf()
/*
print Fahrenheit-Celsius table
for fahr = 0, 20, ..., 300
*/
int main()
{
int 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
fahr = lower;
while(fahr <= upper)
{
celsius = 5 * (fahr-32) / 9;
printf("%d\t%d\n", fahr, celsius);
fahr += step; // fahr = fahr + step;
}
}
/*
gcc temp.c -o temp
./temp
0 -17
20 -6
40 4
60 15
80 26
100 37
120 48
140 60
160 71
180 82
200 93
220 104
240 115
260 126
280 137
300 148
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf()
/*
print Fahrenheit-Celsius table
for fahr = 0, 20, ..., 300
*/
int main()
{
int 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
fahr = lower;
while(fahr <= upper)
{
celsius = 5 * (fahr-32) / 9;
printf("%3d\t%3d\n", fahr, celsius);
fahr += step; // fahr = fahr + step;
}
}
/*
gcc tempr.c -o tempr
./tempr
0 -17
20 -6
40 4
60 15
80 26
100 37
120 48
140 60
160 71
180 82
200 93
220 104
240 115
260 126
280 137
300 148
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#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
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 tempf.c -o tempf
./tempf
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
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf()
int main()
{
printf("5 / 2 = %d\n", 5 / 2); // truncate, print as int
printf("5.0 / 2 = %g\n", 5.0 / 2);
printf("5 / 2.0 = %g\n", 5 / 2.0);
printf("(float) (5 / 2) = %g\n", (float) (5 / 2)); // truncate: (float) 2
printf("(float) 5 / 2 = %g\n", (float) 5 / 2); // ((float) 5) / 2
printf("5 / (float) 2 = %g\n", 5 / (float) 2); // 5 / ((float) 2)
float x = 5, y = 2; // convert int to float in assignments
printf("x / y = %g\n", x / y);
}
/*
gcc truncate.c -o truncate
./truncate
5 / 2 = 2
5.0 / 2 = 2.5
5 / 2.0 = 2.5
(float) (5 / 2) = 2
(float) 5 / 2 = 2.5
5 / (float) 2 = 2.5
x / y = 2.5
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 1-3. Modify the temperature conversion program to print a heading above the table.
#include <stdio.h> // for printf()
/*
print Fahrenheit-Celsius table
for fahr = 0, 20, ..., 300
(floating-point version with heading)
*/
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
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 1-4. Write a program to print the corresponding Celsius to Fahrenheit table.
#include <stdio.h> // for printf()
/*
print Celsius-Fahrenheit table
for celsius = 0, 20, ..., 300
(floating-point version)
*/
int main()
{
float celsius, fahr; // 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
printf("Celsius-Fahrenheit\n");
printf("conversion table\n");
printf("------------------------\n");
celsius = lower;
while(celsius <= upper)
{
fahr = (9.0 / 5.0) * celsius + 32.0;
printf("%3.0f %6.1f\n", celsius, fahr);
celsius += step; // celsius = celsius + step;
}
}
/*
gcc celsius1.c -o celsius1
./celsius1
Celsius-Fahrenheit
conversion table
------------------------
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
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf()
/*
print Celsius-Fahrenheit table
for celsius = -20, -10, 0, 10, ..., 150
(floating-point version)
*/
int main()
{
float celsius, fahr; // main vars used for output
int lower, upper, step; // vars used for computation
lower = -20; // lower limit of temperature table
upper = 150; // upper limit
step = 10; // step size
printf("Celsius-Fahrenheit\n");
printf("conversion table\n");
printf("------------------------\n");
celsius = lower;
while(celsius <= upper)
{
fahr = (9.0 / 5.0) * celsius + 32.0;
printf("%3.0f %6.0f\n", celsius, fahr);
celsius += step; // celsius = celsius + step;
}
}
/*
gcc celsius2.c -o celsius2
./celsius2
Celsius-Fahrenheit
conversion table
------------------------
-20 -4
-10 14
0 32
10 50
20 68
30 86
40 104
50 122
60 140
70 158
80 176
90 194
100 212
110 230
120 248
130 266
140 284
150 302
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Note: while() should be used when testing a condition, for() should be used when counting something.
#include <stdio.h> // for printf()
// print Fahrenheit-Celsius table
int main()
{
int fahr;
for (fahr = 0; fahr <= 300; fahr += 20)
printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
}
/*
gcc fahr.c -o fahr
./fahr
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
*/
for (fahr = 0; fahr <= 300; fahr += 20)
{printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));}
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 1-5. Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0.
#include <stdio.h> // for printf()
// print Fahrenheit-Celsius table in reverse order
int main()
{
int fahr;
for (fahr = 300; fahr >= 0; fahr -= 20)
{printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));}
}
/*
gcc fahrr.c -o fahrr
./fahrr
300 148.9
280 137.8
260 126.7
240 115.6
220 104.4
200 93.3
180 82.2
160 71.1
140 60.0
120 48.9
100 37.8
80 26.7
60 15.6
40 4.4
20 -6.7
0 -17.8
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf()
#define LOWER 0 // lower limit of table
#define UPPER 300 // upper limit
#define STEP 20 // step size
// print Fahrenheit-Celsius table
int main()
{
int fahr;
for (fahr = LOWER; fahr <= UPPER; fahr += STEP)
{printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));}
}
/*
gcc -E define.c // preprocessing
// symbolic constants are replaced by their values:
# 8 "define.c" // what is actually compiled:
int main()
{
int fahr;
for (fahr = 0; fahr <= 300; fahr += 20)
{printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));}
}
gcc define.c -o define // compile
./define // run the program
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
*/