This is a small recursive C program I wrote for a class project.
// PROGRAM: coin.cpp // AUTHOR: Ken Berg // DESCRIPTION: This program calculates the total possible combinations of change for the given input. // When the maximum number of the largest possible coin is removed, decrement the largest // coin value (all the way to zero), and see what combinations can be made from the new // (and larger) remainder. // For example: // 59 is entered by the user // 59 / FIFTY = 1 r 9 // 9 / QUARTER = 0 r 9 // 9 / DIME = 0 r 9 // 9 / NICKEL = 1 r 4 // pennies = 4 // The next way - // 59 / FIFTY = 1 r 9 // 9 / QUARTER = 0 r 9 // 9 / DIME = 0 r 9 // 9 / NICKEL is decremented to 0, therefore r = 9 // pennies = 9 // The next way - // 59 / FIFTY is decremented to 0, therefore r = 59 // 9 / QUARTER = 2 r 9 // 9 / DIME = 0 r 9 // 9 / NICKEL = 1 r 4 // pennies = 4 // And so on... // EXTERNAL FILES: None. // INPUT: User input of change amount. // OUTPUT: The total possible combinations of change for the given input. // WARNINGS: None // SYSTEM: Microsoft Visual C++ 6.0 / Windows 98 // DATE: 5/28/01 // MODIFICATION: My first "solution" came from an algorithm from G. Polya's book "How to Solve it". // It's based on this: // n = the amount entered, A = pennies, B = pennies and nickels, C = pennies, nickels and dimes, // D = pennies, nickels, dimes and quarters, E = pennies, nickels, dimes, quarters, and half dollars. // E(n) = D(n) + E(n - 50) // D(n) = C(n) + D(n - 25) // C(n) = B(n) + C(n - 10) // B(n) = A(n) + B(n - 5) // A(n) = 1 // And when the subscript is negative, it's set equal to 0. // The recursive code was very easy to extract from the algorithm. But since I don't truly // understand recursion, I was uncomfortable calling it "my" code. // 6/1/01 // I got together with a math savvy friend of mine. With his help I was able to understand // the way I calculated the combinations with paper and pencil. From that conversation, // I got the nested for-loop below. #includeReturn to Code#include int coin_counter(int); /********************************************************************************************************/ int main() { int amount = 0; puts("This program computs the total number of possible combinations"); puts("of coinage of amounts less than a dollar."); while (amount !=-1) { puts("Enter an amount between 1 and 99, or enter -1 to quit."); if ((scanf("%d", &amount)) != 1|| amount == -1) { puts("Quitting program"); exit(1); } system("cls"); if (amount == 0) puts("Even I can't calculate change for zero cents.\n\n"); else if (amount > 99) printf("%d is larger than 99.\n\n", amount); else if (amount == 1) puts("There is only one way to produce 1 cent change.\n"); else if (amount >= 2 && amount <= 4) printf("There is only one way to produce %d cents change.\n\n", amount); else if (amount > 4) printf("There are %d ways to produce %d cents change.\n\n", coin_counter(amount), amount); }//while return 0; } /********************************************************************************************************/ int coin_counter(int total) { /* This function starts with the amount passed in to the fifty cent For-Loop. It takes out the maximum number of half-dollars it can. From the remainder the maximum number of quarters are taken out and so on. The pennies are simply whatever is left. When the maximum number of the largest possible coin is removed, decrement the largest coin value, and see what combinations can be made from the new (and larger) remainder. */ typedef enum {PENNY, NICKEL, DIME, QUARTER, FIFTY}; //enumeration to use coin names as array indices int coin_value[] = {1, 5, 10, 25, 50}; //parallel array of coin values int coin_count[5]; //array used to hold the quantity of the various coins int num_ways = 0; //used as a counter to hold the total number of combinations for(coin_count[FIFTY] = total / coin_value[FIFTY]; coin_count[FIFTY] >= 0; --coin_count[FIFTY]) { for(coin_count[QUARTER] = (total - (coin_count[FIFTY] * coin_value[FIFTY])) / coin_value[QUARTER]; coin_count[QUARTER] >= 0 ; --coin_count[QUARTER]) { for(coin_count[DIME] = (total - ((coin_count[FIFTY] * coin_value[FIFTY]) + (coin_count[QUARTER] * coin_value[QUARTER]))) / coin_value[DIME] ; coin_count[DIME] >= 0 ; --coin_count[DIME]) { for(coin_count[NICKEL] = (total - ((coin_count[FIFTY] * coin_value[FIFTY]) + (coin_count[QUARTER] * coin_value[QUARTER]) + (coin_count[DIME] * coin_value[DIME]))) / coin_value[NICKEL] ; coin_count[NICKEL] >= 0 ; --coin_count[NICKEL]) { // total number of pennies is just whatever's left coin_count[PENNY] = total - ((coin_count[FIFTY] * coin_value[FIFTY]) + (coin_count[QUARTER] * coin_value[QUARTER]) + (coin_count[DIME] * coin_value[DIME]) + (coin_count[NICKEL] * coin_value[NICKEL])); ++num_ways; //combination counter } } } } return num_ways; }