|
|
|
Chapter 5Answers to Selected Exercises2. [was #2] (a)1
(b) 1
(c) 1
(d) 1
4. [was #4]
6. [was #12] Yes, the statement is legal. When 10. [was #16] The output is onetwo
since there are no Answers to Selected Programming Projects2. [was #6] #include <stdio.h> int main(void) { int hours, minutes; printf("Enter a 24-hour time: "); scanf("%d:%d", &hours, &minutes); printf("Equivalent 12-hour time: "); if (hours == 0) printf("12:%.2d AM\n", minutes); else if (hours < 12) printf("%d:%.2d AM\n", hours, minutes); else if (hours == 12) printf("%d:%.2d PM\n", hours, minutes); else printf("%d:%.2d PM\n", hours - 12, minutes); return 0; } 4. [was #8; modified] #include <stdio.h> int main(void) { int speed; printf("Enter a wind speed in knots: "); scanf("%d", &speed); if (speed < 1) printf("Calm\n"); else if (speed <= 3) printf("Light air\n"); else if (speed <= 27) printf("Breeze\n"); else if (speed <= 47) printf("Gale\n"); else if (speed <= 63) printf("Storm\n"); else printf("Hurricane\n"); return 0; } 6. [was #10] #include <stdio.h> int main(void) { int check_digit, d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5, first_sum, second_sum, total; printf("Enter the first (single) digit: "); scanf("%1d", &d); printf("Enter first group of five digits: "); scanf("%1d%1d%1d%1d%1d", &i1, &i2, &i3, &i4, &i5); printf("Enter second group of five digits: "); scanf("%1d%1d%1d%1d%1d", &j1, &j2, &j3, &j4, &j5); printf("Enter the last (single) digit: "); scanf("%1d", &check_digit); first_sum = d + i2 + i4 + j1 + j3 + j5; second_sum = i1 + i3 + i5 + j2 + j4; total = 3 * first_sum + second_sum; if (check_digit == 9 - ((total - 1) % 10)) printf("VALID\n"); else printf("NOT VALID\n"); return 0; } 10. [was #14] #include <stdio.h> int main(void) { int grade; printf("Enter numerical grade: "); scanf("%d", &grade); if (grade < 0 || grade > 100) { printf("Illegal grade\n"); return 0; } switch (grade / 10) { case 10: case 9: printf("Letter grade: A\n"); break; case 8: printf("Letter grade: B\n"); break; case 7: printf("Letter grade: C\n"); break; case 6: printf("Letter grade: D\n"); break; case 5: case 4: case 3: case 2: case 1: case 0: printf("Letter grade: F\n"); break; } return 0; } Copyright © 2008, 1996 W. W. Norton & Company, Inc. All rights reserved. |