Golang Program to Find the Area of a Triangle Given All Three Sides


Steps

  • Read all the three sides of the triangle and store them in three separate variables.
  • Using the Heron's formula, compute the area of the triangle.
  • Print the area of the triangle.
Enter first side: 15
Enter second side: 9
Enter third side: 7
Area of the triangle is: 20.69
Enter first side: 5
Enter second side: 6
Enter third side: 7
Area of the triangle is: 14.7

Explanation

  • User must enter all three numbers and store them in separate variables.
  • First, the value of s is found which is equal to (a+b+c)/2
  • Then, the Heron's formula is applied to determine the area of the triangle formed by all three sides.
  • Finally, the area of the triangle is printed.

Example

 Live Demo

package main
import (
   "fmt"
   "math"
)
func main(){
   var a, b, c float64
   fmt.Print("Enter first side of the triangle: ")
   fmt.Scanf("%f", &a)
   fmt.Print("Enter second side of the triangle: ")
   fmt.Scanf("%f", &b)
   fmt.Print("Enter third side of the triangle: ")
   fmt.Scanf("%f", &c)
   s:=(a+b+c)/2
   area:=math.Sqrt(s * (s - a) * (s - b) * (s - c))
   fmt.Printf("Area of the triangle is: %.2f", area)
}

Output

Enter first side of the triangle: 15
Enter second side of the triangle: 9
Enter third side of the triangle: 7
Area of the triangle is: 20.69

Updated on: 31-Jul-2021

310 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements