Minimum Number of Coins Required to Pay Total Amount in C++



Suppose we have a number N, and unlimited number of coins worth 1, 10 and 25 currency coins. Find minimum number of coins we need to use to pay exactly amount N. Suppose N is 14, then number of coins will be 5, as one 10 value coin and four 1 value coin.

To solve this, we have to use these steps −

  • If N < 10, then return N number of 1 value coins
  • If N > 9 and N < 25, then divide the value with 10, and get the result, the remaining will be covered using 1 value coin, add the count to get result
  • If N > 25, then divide it with 25, take the result, when result is < 25, then again do the same task for the second point and so on.

Example

 Live Demo

#include<iostream>
using namespace std;
int countMinCoins(int n) {
   if(n<10)
      return n;
   else if(n > 9 && n < 25){
      int count = n/10;
      count += n%10;
      return count;
   } else {
      int count = n/25;
      return count + countMinCoins(n%25);
   }
}
int main() {
   int n = 88;
   cout << "Minimum number of coins required: " << countMinCoins(n);
}

Output

Minimum number of coins required: 7
Updated on: 2019-12-18T12:46:38+05:30

274 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements