Go Program to Calculate Profit or Loss

This go program uses the actual product cost and the sales amount to calculate profit or loss. It uses an else if statement to print output.

  • If the product cost is greater than the sales price, then loss.
  • If the sales price is higher than the product cost, then the product is profit—otherwise, no profit or loss.
package main

import "fmt"

func main() {

    var pcost, sa, amount int

    fmt.Print("\nEnter the Actual Product Cost = ")
    fmt.Scanln(&pcost)

    fmt.Print("\nEnter the Sale Price = ")
    fmt.Scanln(&sa)

    if sa > pcost {
        amount = sa - pcost
        fmt.Println("Total Profit = ", amount)
    } else if pcost > sa {
        amount = pcost - sa
        fmt.Println("Total Loss = ", amount)
    } else {
        fmt.Println("No Profit No Loss")
    }
}
Go Program to Calculate Profit or Loss 1

Golang Program to Calculate Profit or Loss

In this go program, we used an arithmetic operator to find the profit or loss.

package main

import "fmt"

func main() {

    var pcost, sa, amount int

    fmt.Print("Enter the Actual Product Cost = ")
    fmt.Scanln(&pcost)

    fmt.Print("Enter the Sale Price = ")
    fmt.Scanln(&sa)

    if sa-pcost > 0 {
        amount = sa - pcost
        fmt.Println("Total Profit = ", amount)
    } else if pcost-sa > 0 {
        amount = pcost - sa
        fmt.Println("Total Loss = ", amount)
    } else {
        fmt.Println("No Profit No Loss")
    }
}
SureshMac:GoExamples suresh$ go run profitLoss2.go
Enter the Actual Product Cost = 1900
Enter the Sale Price = 2500
Total Profit =  600
SureshMac:GoExamples suresh$ go run profitLoss2.go
Enter the Actual Product Cost = 2000
Enter the Sale Price = 1340
Total Loss =  660
SureshMac:GoExamples suresh$ go run profitLoss2.go
Enter the Actual Product Cost = 100
Enter the Sale Price = 100
No Profit No Loss