- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Golang program to find the minimum number of coins needed to make a given amount of money

In this golang article we are going to find the minimum number of coins needed to make a given amount of money if a list of coins with different denominations is given. We can use the Greedy Algorithm for this problem.

## Algorithm

**Step 1**− First, we need to import the fmt package.**Step 2**− Then create a function named minCoins() used to calculate the minimum number of coins. The function accepts two arguments one is the array of integers having number of coins and the amount that is to be formed.**Step 3**− Inside this function initialize the number of coins needed equal to 0.**Step 4**− Iterate over the coins array in reverse order. While the remaining amount is greater than or equal to the current coin. Subtract the coin value from the remaining amount and increment the number of coins needed.**Step 5**− Return the number of coins needed.**Step 6**− Now, start the main() function. Inside the main() initialize the array containing coins and store the amount to be made in a variable.**Step 7**− call the function created above and pass in it the array of coins and amount as arguments. Further store the result in another variable and print it on the screen.

## Example

In this Example we will write a go language program to find the minimum number of coins needed to make a given amount of money by using an external user defined function.

package main import "fmt" // function to calculate number of coins func minCoins(coins []int, amount int) int { numCoins := 0 for i := len(coins) - 1; i >= 0; i-- { for amount >= coins[i] { amount -= coins[i] numCoins++ } } return numCoins } func main() { coins := []int{1, 5, 10, 25} amount := 57 result := minCoins(coins, amount) fmt.Printf("Minimum number of coins needed: %d\n", result) }

## Output

Minimum number of coins needed: 5

## Conclusion

We have successfully compiled and executed a go language program to find the minimum number of coins required to make a given amount of money. We have used the Greedy Algorithm here to implement the result. This Algorithm is based on always choosing the largest possible denomination of coins for the remaining amount, which works well for standard denominations of coins used in most currencies. However, it is important to note that the Greedy Algorithm is not always optimal and may not provide the minimum number of coins needed in certain cases where the coin denominations are not standard or follow a specific pattern.