Go Program To Calculate Electricity Bill

In this post, we are going to solve how to calculate electricity bill using Go Programming language.

Hello coders, in this post we are going to solve how to calculate electricity bill using Go Programming language.

Go Program To Calculate Electricity Bill

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

Table Of Contents 👊

Go Program To Calculate Electricity Bill

// Go Program To Calculate Electricity Bill 
package main
import "fmt"

func main(){
  var units, amount, charge, total_amount float64
  
  // Asking for Input
  fmt.Print("Enter the Consumed Units: ")
  fmt.Scan(&units)
  
  // Logic
  if (units > 500){
    amount = units * 12
    charge = 150
  } else if units >= 350{
    amount = units * 10
    charge = 100
  } else if units >= 200{
    amount = units * 8
    charge = 75
  } else if units >= 100{
    amount = units * 5
    charge = 50
  } else{
    amount = units * 3
    charge = 40
  }
  total_amount = amount + charge
  fmt.Println("Total Electricity Bill is", total_amount)
}

Output

Enter the Consumed Units: 450
Total Electricity Bill is 4600

How Does This Program Work ?

  var units, amount, charge, total_amount float64

In this post, we have declared four int data type variables named as units, amount, charges, and total_amount.

  // Asking for Input
  fmt.Print("Enter the Consumed Units: ")
  fmt.Scan(&units)

Then, the user is asked to enter the units. 

  // Logic
  if (units > 500){
    amount = units * 12
    charge = 150
  } else if units >= 350{
    amount = units * 10
    charge = 100
  } else if units >= 200{
    amount = units * 8
    charge = 75
  } else if units >= 100{
    amount = units * 5
    charge = 50
  } else{
    amount = units * 3
    charge = 40
  }

Charges Calculation

if units < 100 – 40.00
if 100 <= units and units < 200 – 50.00
if 200 <= units and units < 350 – 75.00
if 350 <= units and units <=350 – 100.00
if units > 500 = 150.00

  total_amount = amount + charge
  fmt.Println("Total Electricity Bill is", total_amount)

Then, the total amount is calculated using amount and charges. This amount is stored in total_amount variable and printed on the screen using fmt.Println() function.

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.

// - Used for Commenting in Go

Conclusion

I hope after going reading this post, you clearly understood how to calculate electricity bill 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).

Post a Comment