
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find out the 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
#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
- Related Articles
- C++ Program to find out the minimum number of operations required to defeat an enemy
- C++ code to find out the total amount of sales we made
- I have a total of rupees 300 in coins of denomination rupees 1, 2, 5 the number of rupees 2 coins is 3 times the number of rupees 5 coins. The total number of coins is 160. Find the number of coins of each denomination.
- C++ Program to find out the least amount of money required to subscribe to the OTT services
- C/C++ Program for Greedy Algorithm to find Minimum number of Coins
- Problem to Find Out the Maximum Number of Coins that Can be Collected in Python
- Program to find minimum number of steps required to catch the opponent in C++
- I have a total of $\ Rs. 300$ in coins of denominations $\ Rs. 1,\ Rs. 2$ and $\ Rs. 5$. The number of $\ Rs. 2$ coins is 3 times the number of $\ Rs. 5$ coins. The total number of coins is 160. How many coins of each denomination are with me?
- I have a total of \( Rs. 300 \) in coins of denomination \( Rs. 1, Rs. 2 \) and \( Rs. 5 \). The number of \( Rs. 2 \) coins is 3 times the number of \( Rs. 5 \) coins. The total number of coins is 160 . How many coins of each denomination are with me?
- Program to find minimum number of operations required to make one number to another in Python
- Program to find minimum number of hops required to reach end position in Python
- Program to find minimum number of pins required to hang all banners in C++
- Program to find minimum number of flips required to have alternating values in Python
- Program to find minimum number of buses required to reach final target in python
- Find the minimum number of preprocess moves required to make two strings equal in Python

Advertisements