PROGRAM:
package main
import "fmt"
/* group the variable using struct */
type Rectange struct {
length int
width int
}
func Area(r Rectange) int {
return r.length * r.width
}
func Perimeter(r Rectange) int {
return 2 * (r.length * r.width)
}
func main() {
/* create a variable of type Rectangle */
var rect1 Rectange
/* set the individual field of Rectange type */
rect1.length = 5
rect1.width = 8
/* pass Rectangle type to function */
area := Area(rect1)
perimeter := Perimeter(rect1)
fmt.Println("Length-", rect1.length, " Width-", rect1.width,
" Area-", area, " Perimeter-", perimeter)
rect2 := Rectange{15, 30}
area = Area(rect2)
perimeter = Perimeter(rect2)
fmt.Println("Length-", rect2.length, " Width-", rect2.width,
" Area-", area, " Perimeter-", perimeter)}
}
OUTPUT:
Length- 5 Width- 8 Area- 40 Perimeter- 80
Length- 15 Width- 30 Area- 450 Perimeter- 900
No comments:
Post a Comment