I have to create a recursive function that tells you the number of ways a number of cents can be made into change. (Using quarters, dimes nickels, and pennies).
So far, I have a recursive function that does that, however it counts the same combination more than once, so the number is too big. How do I remove the duplicate combinations?
Code:
#include <stdio.h>
//Prototypes
int coins(int);
int main(void){
//Declarations
int num;
//Get user input
printf("Enter an amount of change in cents: ");
scanf("%d", &num); //Change to fgets
//Call function
printf("There are %d ways to make change for %d cents.\n", (coins(num)), num);
}
int coins(int amt){
//Declarations
int ways=0;
//Base Case
if(amt == 0){
return 1;
}
//int ways=0; More efficient after base case.
if(amt >= 1){
ways+=coins(amt-1);
}
if(amt >= 5){
ways+=coins(amt-5);
}
if(amt >= 10){
ways+=coins(amt-10);
}
if(amt >= 25){
ways+=coins(amt-25);
}
return ways;
}
Example:
Input: 17 (cents)
Output: 80 ways **Output should be 6