summaryrefslogtreecommitdiff
path: root/ch1/ftoc.c
diff options
context:
space:
mode:
authorAdam Carpenter <gitlab@53hor.net>2019-07-09 16:00:48 -0400
committerAdam Carpenter <gitlab@53hor.net>2019-07-09 16:00:48 -0400
commit39c0cbcc70aff47e17107826aa76cb525c83b122 (patch)
treedf483af220a11aab49e0e3a76eea54f60efbd424 /ch1/ftoc.c
parent583fc81152e120ec5873f8057f11a894bb8b0a91 (diff)
downloadlearning-c-39c0cbcc70aff47e17107826aa76cb525c83b122.tar.xz
learning-c-39c0cbcc70aff47e17107826aa76cb525c83b122.zip
ch2
Diffstat (limited to 'ch1/ftoc.c')
-rw-r--r--ch1/ftoc.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/ch1/ftoc.c b/ch1/ftoc.c
new file mode 100644
index 0000000..70b994a
--- /dev/null
+++ b/ch1/ftoc.c
@@ -0,0 +1,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);
+}