# How to find the Area of a Circle in Golang?

In this tutorial, we are going to see the Golang program to find the Area of a Circle. The area is the total space covered by any closed figure.

## Formula

Area of Circle - 22 / 7 * r * r
r - radius of a Circle


For example, the radius of a Circle is 10 cm so the Area of a Circle is −

Area = 22 / 7 * 10 * 10
= 4.2857142857143


## Finding the Area of a circle within the function

### Algorithm

• STEP 1 − Declaring the variables for the radius and the area of the float64 data type.

• STEP 2 − Taking the input for the radius from the user

• STEP 3 − Finding the Area using the above formula within the function

• STEP 4 − Printing the result.

#### Time Complexity

O(1) - The time complexity is constant because no matter what the input is the program will take the same time.

#### Space Complexity

O(1) - The variables are static in the program so the space complexity is also constant.

### Example 1

In this example, we are going to find the Area of a Circle within the function.

package main

// fmt package provides the function to print anything
import (
"fmt"
)
func main() {

// declaring the floating variables using the var keyword for
// storing the radius of the circle also a variable area to store Area
var radius, Area float64
fmt.Println("Program to find the Area of a Circle.")

// initializing the radius of a circle
radius = 5

// finding the Area of a Circle
Area = (22 / 7.0) * (radius * radius)

// printing the result
fmt.Println("Radius =", radius, "\nThe Area of the circule =", Area, "cm^2")
fmt.Println("(Finding the Area of a circle within the function)")
}


### Output

Program to find the Area of a Circle.
Radius = 5
The Area of the circule = 78.57142857142857 cm^2
(Finding the Area of a circle within the function)


### Description of code

• var radius, Area float64 − In this line, we are declaring the radius and Area that we are going to use later. As the radius or Area can be in decimal, we have used the float data type.

• Area = (22 / 7.0) * (radius * radius) − In this line of code, we are applying the formula and finding the Area.

• fmt.Println("The Area of a Circle whose radius is", radius, "is", Area, "cm * cm.")

− Printing the Area of a Circle.

## Finding the Area of a circle in the separate function

### Algorithm

• STEP 1 − Declaring the variables for the radius and the area of the float64 data type

• STEP 2 − Taking the input for the radius from the user.

• STEP 3 − Calling the function with the radius as a parameter, and storing the Area the function is returning.

• STEP 4 − Printing the result.

### Example 2

In this example, we are going to find the Area of a Circle by defining the separate function to find the Area.

package main

// fmt package provides the function to print anything
import (
"fmt"
)
func areaOfCircle(radius float64) float64 {
// returning the area by applying the formula
return (22 / 7.0) * (radius * radius)
}
func main() {

// declaring the floating variables using the var keyword for
// storing the radius of circle also a variable area to store Area
var radius, Area float64
fmt.Println("Program to find the Area of a Circle.")

// taking the radius of a Circle as input from the user
fmt.Print("Please enter the radius of a Circle:")
fmt.Scanln(&radius)

// finding the Area of a Circle using a separate function
Area = areaOfCircle(radius)

// printing the result
fmt.Println("The Area of a Circle whose radius is", radius, "is", Area, "cm^2.")
fmt.Println("(Finding the Area of a circle in the separate function)")
}


### Output

Program to find the Area of a Circle.
Please enter the radius of a Circle:20
The Area of a Circle whose radius is 20 is 1257.142857142857 cm^2.
(Finding the Area of a circle in the separate function)


### Description of code

• var radius, Area float64 − In this line, we are declaring the radius and Area that we are going to use later. As the radius, or Area can be in decimal so we have used the float data type.

• fmt.Scanln(&radius) − Taking the input for the radius from the user.

• Area = areaOfCircle(radius) − In this line of code, we are calling the function that is finding the Area of a Circle.

• fmt.Println("The Area of a Circle whose radius is", radius, "is", Area, "cm * cm.")

− Printing the Area of a Circle.

## Conclusion

These are the two ways to find the Area of a Circle in Golang. The second way is much better in terms of modularity and code reusability as we can call that function anywhere in the project. To learn more about go you can explore these tutorials.

Updated on: 29-Aug-2022

415 Views

##### Kickstart Your Career

Get certified by completing the course

Advertisements