# Find out the minimum number of coins required to pay total amount

• Last Updated : 11 Oct, 2021

Given a total amount of N and unlimited number of coins worth 1,  10 and 25 currency coins. Find out the minimum number of coins you need to use to pay exactly amount N.
Examples:

```Input : N = 14
Output : 5
You will use one coin of value 10 and
four coins of value 1.

Input :  N = 88
Output : 7```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Approach:
There are three different cases:

1. If value of N < 10, then coins that have value 1 can only be used for payment.
2. When N > 9 and < 25, then coins that have value 1 and 10 will be used for payment. Here, to minimize the number of coins used, coins with value 10 will be preferred mostly.

3. When N > 24. Then all coins of value 1, 10 and 25 will be used for payment. To minimize the number of coins, the primary aim will be to use coin with value 25 first as much as possible then coin with value 10 and then with value 1.

Below is the implementation of the above approach:

## C++

 `// C++ program to find the minimum number``// of coins required`` ` `#include ``using` `namespace` `std;`` ` `// Function to find the minimum number``// of coins required``int` `countCoins(``int` `n)``{``    ``int` `c = 0;`` ` `    ``if` `(n < 10) {``        ``// counts coins which have value 1``        ``// we will need n coins of value 1``        ``return` `n;``    ``}``    ``if` `(n > 9 && n < 25) {``        ``// counts coins which have value 1 and 10``        ``c = n / 10 + n % 10;``        ``return` `c;``    ``}``    ``if` `(n > 24) {``        ``// counts coins which have value 25``        ``c = n / 25;`` ` `        ``if` `(n % 25 < 10) {`` ` `            ``// counts coins which have value 1 and 25``            ``c = c + n % 25;``            ``return` `c;``        ``}`` ` `        ``if` `(n % 25 > 9) {``            ``// counts coins which have value 1, 10 and 25``            ``c = c + (n % 25) / 10 + (n % 25) % 10;``            ``return` `c;``        ``}``    ``}``}`` ` `// Driver Code``int` `main()``{``    ``int` `n = 14;`` ` `    ``cout << countCoins(n);`` ` `    ``return` `0;``}`

## Java

 `// Java program to find the minimum number``// of coins required`` ` `class` `GFG {`` ` `    ``// Function to find the minimum number``    ``// of coins required``    ``static` `int` `countCoins(``int` `n)``    ``{``        ``int` `c = ``0``;``        ``if` `(n < ``10``) {``            ``// counts coins which have value 1``            ``// we will need n coins of value 1``            ``return` `n;``        ``}``        ``if` `(n > ``9` `&& n < ``25``) {`` ` `            ``// counts coins which have value 1 and 10``            ``c = n / ``10` `+ n % ``10``;`` ` `            ``return` `c;``        ``}``        ``if` `(n > ``24``) {``            ``// counts coins which have value 25``            ``c = n / ``25``;`` ` `            ``if` `(n % ``25` `< ``10``) {``                ``// counts coins which have value 1 and 25``                ``c = c + n % ``25``;`` ` `                ``return` `c;``            ``}``            ``if` `(n % ``25` `> ``9``) {``                ``// counts coins which have value 1, 10 and 25``                ``c = c + (n % ``25``) / ``10` `+ (n % ``25``) % ``10``;`` ` `                ``return` `c;``            ``}``        ``}`` ` `        ``return` `c;``    ``}`` ` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `n = ``14``;``        ``System.out.println(countCoins(n));``    ``}``}`

## Python3

 `# Python3 program to find the minimum number``# of coins required`` ` `# Function to find the minimum number``# of coins required``def` `countCoins(n):`` ` `    ``c ``=` `0`` ` `    ``if` `(n < ``10``):``         ` `        ``# counts coins which have value 1``        ``# we will need n coins of value 1``        ``return` `n``     ` `    ``if` `(n > ``9` `and` `n < ``25``):``         ` `        ``# counts coins which have value 1 and 10``        ``c ``=` `n ``/``/` `10` `+` `n ``%` `10``        ``return` `c`` ` `    ``if` `(n > ``24``):``         ` `        ``# counts coins which have value 25``        ``c ``=` `n ``/``/` `25`` ` `        ``if` `(n ``%` `25` `< ``10``):`` ` `            ``# counts coins which have value``            ``# 1 and 25``            ``c ``=` `c ``+` `n ``%` `25``            ``return` `c`` ` `        ``if` `(n ``%` `25` `> ``9``):``             ` `            ``# counts coins which have value``            ``# 1, 10 and 25``            ``c ``=` `(c ``+` `(n ``%` `25``) ``/``/` `10` `+``                     ``(n ``%` `25``) ``%` `10``)``            ``return` `c`` ` `# Driver Code``n ``=` `14`` ` `print``(countCoins(n))`` ` `# This code is contributed by mohit kumar`

## C#

 `// C# program to find the minimum number ``// of coins required ``using` `System;`` ` `class` `GFG``{ `` ` `    ``// Function to find the minimum number ``    ``// of coins required ``    ``static` `int` `countCoins(``int` `n) ``    ``{ ``        ``int` `c = 0; ``        ``if` `(n < 10) ``        ``{ ``            ``// counts coins which have value 1 ``            ``// we will need n coins of value 1 ``            ``return` `n; ``        ``} ``        ``if` `(n > 9 && n < 25)``        ``{ `` ` `            ``// counts coins which have value 1 and 10 ``            ``c = n / 10 + n % 10; `` ` `            ``return` `c; ``        ``} ``        ``if` `(n > 24)``        ``{ ``            ``// counts coins which have value 25 ``            ``c = n / 25; `` ` `            ``if` `(n % 25 < 10) ``            ``{ ``                ``// counts coins which have value 1 and 25 ``                ``c = c + n % 25; `` ` `                ``return` `c; ``            ``} ``            ``if` `(n % 25 > 9)``            ``{ ``                ``// counts coins which have value 1, 10 and 25 ``                ``c = c + (n % 25) / 10 + (n % 25) % 10; `` ` `                ``return` `c; ``            ``} ``        ``} `` ` `        ``return` `c; ``    ``} `` ` `    ``// Driver Code ``    ``public` `static` `void` `Main() ``    ``{ ``        ``int` `n = 14; ``        ``Console.WriteLine(countCoins(n)); ``    ``}``} `` ` `// This code is contributed by Ryuga`

## PHP

 ` 9 && ``\$n` `< 25) ``    ``{`` ` `        ``// counts coins which have value 1 and 10``        ``\$c` `= (int)(``\$n` `/ 10 + ``\$n` `% 10);`` ` `        ``return` `\$c``;``    ``}``     ` `    ``if` `(``\$n` `> 24) ``    ``{``        ``// counts coins which have value 25``        ``\$c` `= (int)(``\$n` `/ 25);`` ` `        ``if` `(``\$n` `% 25 < 10) ``        ``{``             ` `            ``// counts coins which have value 1 and 25``            ``\$c` `= ``\$c` `+ ``\$n` `% 25;`` ` `            ``return` `\$c``;``        ``}``        ``if` `(``\$n` `% 25 > 9)``        ``{``            ``// counts coins which have value 1, 10 and 25``            ``\$c` `= ``\$c` `+ (``\$n` `% 25) / 10 + (``\$n` `% 25) % 10;`` ` `            ``return` `\$c``;``        ``}``    ``}`` ` `    ``return` `\$c``;``}`` ` `// Driver Code``\$n` `= 14;``echo``(countCoins(``\$n``));`` ` `// This code is contributed Code_Mech`

## Javascript

 ``
Output:
`5`

My Personal Notes arrow_drop_up