Go Program To Count Total Number of Notes in a Given Amount

In this post we will learn how to calculate total number of notes in a given amount.

Hello coders, in this post we will learn how to calculate total number of notes in a given amount.

Go Program To Count Total Number of Notes

We will be using 1, 2, 5, 10, 50, 100, 500, 2000 Rupee Notes in this program. 

So, without further delay, let's begin this tutorial.

Table Of Contents 👊

Go Program To Count Total Number of Notes

// Go Program To Count Total Number of Notes in a Given Amount
package main
import "fmt"

func main(){
  
  notes := [9]int{2000, 500, 100, 50, 20, 10, 5, 2, 1} 
  
  var amount, i int 
  
  // Asking for Input
  fmt.Print("Enter the Amount: ")
  fmt.Scan(&amount)
  
  temp := amount
  for i = 0; i < 9; i++{
    fmt.Println(notes[i], "Notes =", temp/notes[i])
    temp = temp % notes[i]
  }
}

Output

Enter the Amount: 18477
2000 Notes = 9
500 Notes = 0
100 Notes = 4
50 Notes = 1
20 Notes = 1
10 Notes = 0
5 Notes = 1
2 Notes = 1
1 Notes = 0

How Does This Program Work ?

  var amount, i int 

In this program integer data type variable amount and i.

  // Asking for Input
  fmt.Print("Enter the Amount: ")
  fmt.Scan(&amount)

Then, the user is asked to enter the value of amount.

  temp := amount
  for i = 0; i < 9; i++{
    fmt.Println(notes[i], "Notes =", temp/notes[i])
    temp = temp % notes[i]
  }

We have used for loop to iterate the notes array and divide the amount with each array item.

Some of the used terms in this program are as follows:- 

package main - packages are a collection of Go source files that reside in the same directory. The line package main specifies that this file belongs to the main package.

Every Executable Go Application must contain the main function.

fmt.Println() - The fmt.Println() function in Go language is used to display output.

fmt.Scan() - The fmt.Scan() function is used to take input from the user in Go Programming language.

:= - The := syntax is shorthand for declaring and initializing a variable.

for - for is Go's only looping construct. for without a condition will loop repeatedly until you break out of the loop or return the enclosing function.

// - Used for Commenting in Go

Go Program To Count Total Notes Using Functions

// Go Program To Count Total Number of Notes Using Functions
package main
import "fmt"

func counting(amount int){
  notes := [9]int{2000, 500, 100, 50, 20, 10, 5, 2, 1}
  temp := amount 
  for i := 0; i < 9; i++{
    fmt.Println(notes[i], "Notes =", temp/notes[i])
    temp = temp % notes[i]
  }
}

func main(){
  
  var amount int
  // Asking for Input
  fmt.Print("Enter the Amount: ")
  fmt.Scan(&amount)
  counting(amount)
}

Output

Enter the Amount: 4125        
2000 Notes = 2
500 Notes = 0
100 Notes = 1
50 Notes = 0
20 Notes = 1
10 Notes = 0
5 Notes = 1
2 Notes = 0
1 Notes = 0

Conclusion

I hope after reading this post, you understood how to count total number of notes using Go Programming language.

If you have any doubt regarding the topic, let us know in the comment section.

Sloth Coders is a Learning Platform for Coders, Programmers and Developers to learn from the basics to advance of Coding of different langauges(python, Java, Javascript and many more).

إرسال تعليق