How to find the volume of a capsule in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "math"
)

// V = π r² h + (4/3) π r³
func capsuleVolume(r, h float64) float64 {
    pi := math.Pi
    
    return pi * r * r * h + (4.0/3.0) * pi * r * r * r
}

func main() {
    r := 6.0
    h := 11.0

    volume := capsuleVolume(r, h)
    
    fmt.Printf("Capsule volume = %.2f\n", volume)
}



/*
run:

Capsule volume = 2148.85

*/

 



answered Jan 12 by avibootz
...