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.
3 min read

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).

قد تُعجبك هذه المشاركات

  • Hello coders, in this post we are going to solve how to calculate electricity bill using Go Programming language. So, without further delay, let's begin this …
  • Hello coders, in this post we will learn how to calculate cube of a number using Go Programming language. A Cube Number is a number multiplied by itself 3 times. …
  • Hello coders, in this post we will learn how to calculate compound interest using Go Programming language. Compound Interest is the interest calculated based …
  • Hello coders, in this post we will know how to count number of digits in an integer using Go Programming language. We will be using for loop to count the num…
  • In this post, we will learn how to write hello world program in Go language. Hello, World! is the first program any coder writes to know the basic syntax of any new …
  • Hello coders, in this post we will learn how to add two numbers using Go Programming language. We will be using different methods to add two numbers in Go language.&…

إرسال تعليق