/* * Program to make change: * * Input: Non-negative integer N, number of pennies. * * Output: Error message if N < 0; otherwise numbers of dollars, quarters, * dimes, nickels, pennies to add up to N. The idea is to use the minimum * number of smaller coins (e.g., one dollar rather than four quarters). */ #include /* example of using #define to give names to constant values */ #define PENNIES_IN_DOLLAR 100 #define PENNIES_IN_QUARTER 25 #define PENNIES_IN_DIME 10 #define PENNIES_IN_NICKEL 5 int main(void) { int N; int dollars; int quarters; int dimes; int nickels; int pennies; printf("enter number of pennies\n"); if (scanf("%d", &N) != 1) { printf("Not a number\n"); } else { printf("Input: %d\n", N); if (N < 0) { printf("The number cannot be negative\n"); } else { /* divide N by PENNIES_IN_DOLLAR to give dollars */ dollars = N / PENNIES_IN_DOLLAR; N = N % PENNIES_IN_DOLLAR; /* get remainder */ /* divide remainder by PENNIES_IN_QUARTER .... */ quarters = N / PENNIES_IN_QUARTER; N = N % PENNIES_IN_QUARTER; /* divide remainder by PENNIES_IN_DIME ... */ dimes = N / PENNIES_IN_DIME; N = N % PENNIES_IN_DIME; /* divide remainder by PENNIES_IN_NICKEL ... */ nickels = N / PENNIES_IN_NICKEL; N = N % PENNIES_IN_NICKEL; pennies = N; /* print results */ printf("%d dollars\n", dollars); printf("%d quarters\n", quarters); printf("%d dimes\n", dimes); printf("%d nickels\n", nickels); printf("%d pennies\n", pennies); } } return 0; }