ProgrammingVery hardCompetitive coding (CP)

How many ways are there to make 10 from unlimited coins of value 2, 3 and 5? Compute with this program.

How many ways are there to make 10 from unlimited coins of value 2, 3 and 5? Compute with this program.
#include <stdio.h>
int ways(int n, int k) {
    if (n == 0) return 1;
    if (n < 0 || k == 0) return 0;
    int coins[] = {2, 3, 5};
    return ways(n - coins[k - 1], k) + ways(n, k - 1);
}
int main(void) {
    printf("%d", ways(10, 3));
}
  1. A. 4
  2. B. 3
  3. C. 5
  4. D. 6

Correct answer: A. 4

The recursion counts multisets of coins that total 10: 2+2+2+2+2, 2+3+5, 5+5, and 2+2+3+3. These are the four ways.

More C Programming questions

Exams that ask this topic