Skip to content

Instantly share code, notes, and snippets.

@juliakm
Created February 12, 2020 14:34
Show Gist options
  • Save juliakm/9e031f7415aa621305aeac6412f28b5e to your computer and use it in GitHub Desktop.
Save juliakm/9e031f7415aa621305aeac6412f28b5e to your computer and use it in GitHub Desktop.
Get Programming with Go (6.4)
// Save some money to buy a gift for your friend.
// Write a program that randomly places nickels ($0.05), dimes ($0.10), and quarters ($0.25) into an empty piggy bank
// until it contains at least $20.00. Display the running balance of the piggy bank after each deposit,
// formatting it with an appropriate width and precision.
package main
import (
"fmt"
"math/rand"
)
func main() {
for sum := 0.0; sum < 21.0; sum++ {
if num := rand.Intn(3); num == 0 {
sum += 0.05
} else if num == 1 {
sum += 0.10
} else {
sum += 0.25
}
fmt.Printf("$%.2f\n", sum)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment