/* * Program to calculate Acme's price for ball caps, as described * in the homework writeup. */ #include int main(void) { int caps; printf("how many caps?\n"); if (scanf("%d", &caps) != 1) { printf("invalid input (not integer)\n"); return 1; } if (caps <= 0) { printf("invalid input (not positive)\n"); return 1; } if (caps <= 50) { printf("price $%d\n", 100); if (caps < 50) { printf("you could get 50 caps for the same price\n"); } } else if (caps <= 200) { /* how many groups of 5, in addition to the minimum? */ int groups = (caps - 50) / 5; int price = 100 + (groups * 5); if ((caps % 5) != 0) { price += 5; } printf("price $%d\n", price); if ((caps % 5) != 0) { /* trick to round up to next multiple of 5 */ printf("you could get %d caps for the same price\n", ((caps + 5-1) / 5) * 5); } } else { printf("you qualify for the volume discount!\n"); int discount_groups = (caps - 200) / 5; int price = /* cost for first 50 caps */ 100 + /* cost for next 150 caps */ 150 + /* cost for remaining caps (almost) */ discount_groups * 4; if ((caps % 5) != 0) { price += 4; } printf("price $%d\n", price); if ((caps % 5) != 0) { /* trick to round up to next multiple of 5 */ printf("you could get %d caps for the same price\n", ((caps + 5-1) / 5) * 5); } } return 0; }