Golang : Find change in a combination of coins example
If you are planning to build a vending machine, self-service cashier machine or any machines that deal with money. Chances are the machine will need to have the ability to find out how much change in coins needed to be given back to the customers. Before deciding how much change needs to be given back, first the program needs to breakdown or know the combination of coins of a given value.
For example, given a 50% value of a dollar, what is the combination of coins that will make up the 50% value of a dollar? Mathematically, it should be 2 quarters.
The code that follows is an example program that will take in an integer value from 1 to 100 and prints out the change combination.
Here you go!
package main
import (
"fmt"
"os"
)
var (
amount, originalAmount, quarters, dimes, nickels, pennies int
)
func sanityCheck(input, min, max int) {
if !((input >= min) && (input <= max)) {
fmt.Printf("Input parameter must in between %d and %d.\n", min, max)
os.Exit(-1)
}
}
func main() {
fmt.Println("Enter an integer value from 1 to 100 : ")
fmt.Println("and I will show you a combination of coins")
fmt.Println("that equals to the number.")
_, err := fmt.Scanf("%d", &amount)
if err != nil {
fmt.Println(err)
}
sanityCheck(amount, 1, 100)
fmt.Println("You have entered : ", amount)
originalAmount = amount
quarters = amount / 25
amount = amount % 25
dimes = amount / 10
amount = amount % 10
nickels = amount / 5
amount = amount % 5
pennies = amount
fmt.Println("Original amount entered : ", originalAmount)
fmt.Println("Has a combination in coins of : ")
fmt.Println("Quarters : ", quarters)
fmt.Println("Dimes : ", dimes)
fmt.Println("Nickels : ", nickels)
fmt.Println("Pennies : ", pennies)
}
Sample output:
Enter an integer value from 1 to 100 :
and I will show you a combination of coins
that equals to the number.
You have entered : 100
Original amount entered : 100
Has a combination in coins of :
Quarters : 4
Dimes : 0
Nickels : 0
Pennies : 0
Enter an integer value from 1 to 100 :
and I will show you a combination of coins
that equals to the number.
2
You have entered : 2
Original amount entered : 2
Has a combination in coins of :
Quarters : 0
Dimes : 0
Nickels : 0
Pennies : 2
Enter an integer value from 1 to 100 :
and I will show you a combination of coins
that equals to the number.
88
You have entered : 88
Original amount entered : 88
Has a combination in coins of :
Quarters : 3
Dimes : 1
Nickels : 0
Pennies : 3
References:
https://socketloop.com/tutorials/golang-integer-is-between-a-range
https://www.socketloop.com/tutorials/golang-how-to-read-integer-value-from-standard-input
https://www.socketloop.com/tutorials/golang-how-to-check-if-input-from-os-args-is-integer
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.5k Golang : Convert(cast) string to int64
+10k Golang : Channels and buffered channels examples
+20.1k Golang : How to run your code only once with sync.Once object
+6.9k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+7.3k Golang : Check if one string(rune) is permutation of another string(rune)
+20.2k Golang : Reset or rewind io.Reader or io.Writer
+8k Javascript : Put image into Chrome browser's console
+10.6k Golang : Create matrix with Gonum Matrix package example
+11.2k Golang : Calculate Relative Strength Index(RSI) example
+14.4k Golang : Simple word wrap or line breaking example
+55.4k Golang : Unmarshal JSON from http response
+7.5k Golang : Shuffle strings array