This repository has been archived by the owner on Nov 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathchange.go
101 lines (90 loc) · 2.09 KB
/
change.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// The user enters a cost and then the amount of money given. The program will
// figure out the change and the number of quarters, dimes, nickels, pennies
// needed for the change.
package main
import (
"fmt"
"math"
)
func main() {
var price float64
var payment float64
fmt.Println("What is the price of the purchase? ($)")
_, err := fmt.Scanf("%f", &price)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("What amount is given? ($)")
_, err = fmt.Scanf("%f", &payment)
if err != nil {
fmt.Println("Error:", err)
return
}
if payment < price {
fmt.Printf("Not enough! $%.2f is less than $%.2f.", payment, price)
return
}
diff := payment - price
if diff >= 1000 {
fmt.Println("Overpayment - do you have any smaller bills?")
return
}
if math.Abs(diff) < 0.01 {
fmt.Println("No change due - paid with exact change")
return
}
// Bills
hundreds := math.Floor(diff / 100)
leftover := diff - hundreds*100
fifties := math.Floor(leftover / 50)
leftover -= fifties * 50
twenties := math.Floor(leftover / 20)
leftover -= twenties * 20
tens := math.Floor(leftover / 10)
leftover -= tens * 10
fives := math.Floor(leftover / 5)
leftover -= fives * 5
ones := math.Floor(leftover)
leftover -= ones
// Coins (less than 1 dollar)
leftover *= 100
quarters := math.Floor(leftover / 25)
leftover -= quarters * 25
dimes := math.Floor(leftover / 10)
leftover -= dimes * 10
nickels := math.Floor(leftover / 5)
leftover -= nickels * 5
pennies := math.Floor(leftover + 0.5)
fmt.Println("Change due:")
if hundreds > 0 {
fmt.Println("$100 bills:", hundreds)
}
if fifties > 0 {
fmt.Println("$50 bills: ", fifties)
}
if twenties > 0 {
fmt.Println("$20 bills: ", twenties)
}
if tens > 0 {
fmt.Println("$10 bills: ", tens)
}
if fives > 0 {
fmt.Println("$5 bills: ", fives)
}
if ones > 0 {
fmt.Println("$1 bills: ", ones)
}
if quarters > 0 {
fmt.Println("Quarters: ", quarters)
}
if dimes > 0 {
fmt.Println("Dimes: ", dimes)
}
if nickels > 0 {
fmt.Println("Nickels: ", nickels)
}
if pennies > 0 {
fmt.Println("Pennies: ", pennies)
}
}