Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Solution: circle STDIN with math

  • math

Of course you don't need to type in the value if PI yourself. There is a module called math that provides you the value at a much better precision. There is also a function called Pow that can rasie any number to any power.

package main

import (
	"fmt"
	"math"
	"strconv"
)

func main() {
	fmt.Println(math.Pi)

	var radiusStr string
	fmt.Print("radius: ")
	fmt.Scan(&radiusStr)

	radius, err := strconv.ParseFloat(radiusStr, 64)
	if err == nil {
		fmt.Println(radius)
		area := math.Pi * math.Pow(radius, 2)
		circumference := 2 * math.Pi * radius
		fmt.Printf("Area: %v\n", area)
		fmt.Printf("Cirumference: %v\n", circumference)
	}
}