{"content":{"id":60,"title":"K\u0026R Solutions - 1.5","body":"\u003cbr\u003e\r\n\u003cp\u003e\u003cb\u003eQ:\u003c/b\u003e Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0. \u003c/p\u003e\r\n\u003cbr\u003e\r\n\u003cp\u003e\u003cb\u003eA:\u003c/b\u003e\u003c/p\u003e\r\n\u003cpre\u003e\u003ccode class=\"cpp\"\u003e#include \u0026ltstdio.h\u0026gt\r\n\r\nint main(void)\r\n{\r\n int fahr;\r\n\r\n for (fahr = 300; fahr \u003e= 0; fahr -= 20)\r\n printf(\"%3d %6.1f\\n\", fahr, (5.0/9.0)*(fahr-32));\r\n\r\n return 0;\r\n}\r\n\u003c/pre\u003e\u003c/code\u003e\r\n\u003cpre\u003e\u003ccode class=\"cpp\"\u003e300 148.9\r\n280 137.8\r\n260 126.7\r\n240 115.6\r\n220 104.4\r\n200 93.3\r\n180 82.2\r\n160 71.1\r\n140 60.0\r\n120 48.9\r\n100 37.8\r\n 80 26.7\r\n 60 15.6\r\n 40 4.4\r\n 20 -6.7\r\n 0 -17.8\r\n\u003c/code\u003e\u003c/pre\u003e","publication_date":"2018-06-16T00:00:00.000Z","created_at":"2018-06-06T03:05:04.000Z","updated_at":"2018-06-18T03:31:49.000Z","user_id":1,"rating":null},"tags":"\u003ca class=\"changeable-title\" href=\"/q?tag=k%26r\"\u003ek\u0026amp;r\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=programming\"\u003eprogramming\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=answers\"\u003eanswers\u003c/a\u003e \u003ca class=\"changeable-title\" href=\"/q?tag=c\"\u003ec\u003c/a\u003e"}

K&R Solutions - 1.5


Q: Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0.


A:

#include <stdio.h>

int main(void)
{
  int fahr;

  for (fahr = 300; fahr >= 0; fahr -= 20)
    printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));

  return 0;
}
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