In this Go Program to Calculate Electricity Bill, we assume that your electricity board charges different tariffs based on the unit’s consumption. For this Go example, we use the Else If statement,
- else if units <= 100 – Charges for first fifty units are 130 (2.60 * 50). So, we added that amount and removed those fifty units from the total.
- else if units <= 200 – Charges for first fifty units are 130, and 50 to 100 unit changes are 162.50 (3.25 * 50). So, we added that amount and eliminated those hundred units from the total.
- else – Charges for first fifty units are 130, 50 to 100 are 162.50, and 100 to 200 are 526 (5.65 * 100). So, we appended that amount and excluded those two hundred units from the total.
package main import "fmt" func main() { var units, surCharge, amount, totAmount float64 fmt.Print("Enter the Consumed Units = ") fmt.Scanln(&units) if units < 50 { amount = units * 2.60 surCharge = 25 } else if units <= 100 { amount = 130 + ((units - 50) * 3.25) surCharge = 35 } else if units <= 100 { amount = 130 + 162.50 + ((units - 100) * 5.26) surCharge = 45 } else { amount = 130 + +162.50 + 526 + ((units - 200) * 7.75) surCharge = 55 } totAmount = amount + surCharge fmt.Println("Electricity Bill = ", totAmount) }
Golang Program to Calculate Electricity Bill
In this Go example, we assume you have uniform rates or fixed rates for the units and calculates the electricity bill.
package main import "fmt" func main() { var units, surCharge, amount, totAmount float64 fmt.Print("Enter the Consumed Units = ") fmt.Scanln(&units) if units > 500 { amount = units * 12.65 surCharge = 125 } else if units >= 300 { amount = units * 10.75 surCharge = 100 } else if units >= 200 { amount = units * 8.26 surCharge = 85 } else if units >= 100 { amount = units * 5.98 surCharge = 65 } else { amount = units * 3.85 surCharge = 45 } totAmount = amount + surCharge fmt.Println("Electricity Bill = ", totAmount) }
SureshMac:Goexamples suresh$ go run electricity2.go
Enter the Consumed Units = 720
Electricity Bill = 9233
SureshMac:Goexamples suresh$ go run electricity2.go
Enter the Consumed Units = 400
Electricity Bill = 4400
SureshMac:Goexamples suresh$ go electricity2.go
Enter the Consumed Units = 65
Electricity Bill = 295.25