How to calculate the area of a triangle using Heron’s formula in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "math"
)

// Function to calculate area using Heron's formula
func heronArea(a, b, c float64) float64 {
    s := (a + b + c) / 2.0 // semi-perimeter

    return math.Sqrt(s * (s - a) * (s - b) * (s - c))
}

func main() {
    a, b, c := 6.0, 9.0, 13.0

    // Check the validity of the triangle
    if a+b > c && a+c > b && b+c > a {
        area := heronArea(a, b, c)
        fmt.Printf("Area of the triangle = %.4f\n", area)
    } else {
        fmt.Println("Invalid triangle sides!")
    }
}


/*
run:

Area of the triangle = 23.6643

*/

 



answered Dec 15, 2025 by avibootz
edited Dec 15, 2025 by avibootz
...