You could do it with quite a few less variables.
Code:
#include <stdio.h>
int main(void) {
int cents, num;
double amount, GST;
printf("Please enter the amount to be paid: $");
scanf("%lf", &amount);
GST = amount * 0.13 + .005;
amount += GST;
printf("GST: %.2f\n", GST);
printf("Balance owing: $%.2f\n", amount);
cents = amount * 100 + 0.5;
num = cents / 100;
cents = cents % 100;
printf("Loonies required: %d, balance owing $%.2f\n",
num, cents / 100.0);
num = cents / 25;
cents = cents % 25;
printf("Quarters required: %d, balance owing $%.2f\n",
num, cents / 100.0);
num = cents / 10;
cents = cents % 10;
printf("Dimes required: %d, balance owing $%.2f\n",
num, cents / 100.0);
num = cents / 5;
cents = cents % 5;
printf("Nickels required: %d, balance owing $%.2f\n",
num, cents / 100.0);
printf("Pennies required: %d, balance owing $0.00\n",
cents);
return 0;
}
Noticing how repetitive it is leads to another possibility:
Code:
#include <stdio.h>
struct {
const char *name;
int value;
} coins[] = {
{ "Loonies", 100 },
{ "Quarters", 25 },
{ "Dimes", 10 },
{ "Nickels", 5 },
{ "Pennies", 1 },
{ NULL, 0 } // indicates end of list
};
int main(void) {
printf("Please enter the amount to be paid: $");
double amount;
scanf("%lf", &amount);
double GST = amount * 0.13 + .005;
printf("GST: %.2f\n", GST);
amount += GST;
printf("Balance owing: $%.2f\n", amount);
int cents = amount * 100 + 0.5;
for (size_t i = 0; coins[i].name; i++) {
int num = cents / coins[i].value;
cents = cents % coins[i].value;
printf("%s required: %d, balance owing $%.2f\n",
coins[i].name, num, cents / 100.0);
}
return 0;
}