Write a Go Program to Count the total number of currency notes in a given amount using Arrays and For loop. First, we declared an integer array that holds the available notes. Next, we used for loop (for i := 0; i < 8; i++) to iterate the notes array and divides the amount with each array item. Then, we update the count by removing that cash amount from the original.
package main import "fmt" func main() { notes := [8]int{500, 100, 50, 20, 10, 5, 2, 1} var amount int fmt.Print("Enter the Total Amount of Cash = ") fmt.Scanln(&amount) temp := amount for i := 0; i < 8; i++ { fmt.Println(notes[i], " Notes = ", temp/notes[i]) temp = temp % notes[i] } }
Enter the Total Amount of Cash = 5698
500 Notes = 11
100 Notes = 1
50 Notes = 1
20 Notes = 2
10 Notes = 0
5 Notes = 1
2 Notes = 1
1 Notes = 1
Go Program to Count Total Notes in an Amount using Functions
In this Golang program, we created a (countingNotes(amount int)) function that counts and prints the currency notes in given cash.
package main import "fmt" func countingNotes(amount int) { notes := [8]int{500, 100, 50, 20, 10, 5, 2, 1} temp := amount for i := 0; i < 8; i++ { fmt.Println(notes[i], " Notes = ", temp/notes[i]) temp = temp % notes[i] } } func main() { var amount int fmt.Print("Enter the Total Amount of Cash = ") fmt.Scanln(&amount) countingNotes(amount) }
