Created
February 12, 2020 14:34
-
-
Save juliakm/9e031f7415aa621305aeac6412f28b5e to your computer and use it in GitHub Desktop.
Get Programming with Go (6.4)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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