Struct and Interface Issue

We had written code as -

package main
import "fmt"

type Rectangle struct {
    Length, Width float32
}

type Circle struct {
    Radius float32
}

func (r *Rectangle) Area() float32 {
    return r.Length * r.Width
}

func (r *Rectangle) Perimeter() float32 {
    return 2 * (r.Length + r.Width)
}

func (c *Circle) Area() float32 {
    const PI float32 = 3.144444444444
    return PI * (c.Radius  * c.Radius)
}

func (c *Circle ) Circumference() float32 {
    const PI float32 =  3.14444444444
    return 2 * (PI * c.Radius)
}

func main() {
var r Rectangle   
var c Circle 
    var first float32
    fmt.Scanln(&first)
    var second float32
    fmt.Scanln(&second)
    var third float32
    fmt.Scanln(&third)
r.Length = first
r.Width = second
c.Radius = third
fmt.Println(r.Area())
fmt.Println(r.Perimeter())
fmt.Println(c.Area())
fmt.Println(c.Circumference())
}

Here first is 5, second is 6 and third is 5.

Our issue is in circle output seems some floating issue nit sure where we went wrong hence need suggestion-

Your Output
30
22
78.611115
31.444445

Expected Output
30
22
78.53981633974483
31.41592653589793

nit sure where we went wrong

You’ve defined PI as 3.1444444, but that is not the value of PI. Your expected output uses the correct value of PI. PI is defined in the math package, so use it from there: math package - math - Go Packages

Please wait for @NobbZ’s explanation first or just wait for @petrus’s copy’n’pase code below :point_down:

It is resolved thanks after PI value :slight_smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.