You are reading the article **How To Find The Area Of A Circle In Golang?** updated in February 2024 on the website Tai-facebook.edu.vn. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. *Suggested March 2024 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 CircleFor 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 ComplexityO(1) – The time complexity is constant because no matter what the input is the program will take the same time.

Space ComplexityO(1) – The variables are static in the program so the space complexity is also constant.

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

import

(

“fmt”

)

func

main

(

)

{

var

radius

,

Area

float64

fmt

.

Println

(

“Program to find the Area of a Circle.”

)

radius

=

5

Area

=

(

22

/

7.0

)

*

(

radius

*

radius

)

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 2In this example, we are going to find the Area of a Circle by defining the separate function to find the Area.

package

main

import

(

"fmt"

)

funcareaOfCircle

(

radius float64)

float64{

return

(

22

/

7.0

)

*

(

radius*

radius)

}

funcmain

(

)

{

var

radius,

Area

float64fmt

.

Println(

"Program to find the Area of a Circle."

)

fmt

.

Print(

"Please enter the radius of a Circle:"

)

fmt

.

Scanln(

&

radius)

Area

=

areaOfCircle

(

radius)

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.

ConclusionThese 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.

You're reading __How To Find The Area Of A Circle In Golang?__

## Golang Program To Calculate The Volume And Area Of The Cylinder

In this tutorial, we will be discussing the approach to calculate the volume and area of a cylinder in Golang programming using its height and radius.

But before writing the code for this, let’s briefly discuss the cylinder and its volume and area.

CylinderA cylinder is a three-dimensional figure whose base has a circular shape. The distance between the two bases is known as the cylinder’s height ‘h’ and the radius of the base is denoted by ‘r’. A cold drink can is a good example of a cylinder.

Volume of a cylinderThe capacity of the cylinder is generally termed its volume. Calculating the volume of a cylinder can be beneficial in situations where we want to know the capacity of a bottle or a container.

$$mathrm{Volume , =, pi * left ( r right )^{2} * h}$$

Area of a cylinderThe total area enclosed by the cylinder is known as the surface area of a cylinder.

$$mathrm{Area , =, 2ast pi * r * left ( h+r right )}$$

Example

Height = 7, Radius = 5

Volume of cylinder = 550

Area of cylinder = 377.1428571428571

Explanation

Volume of cylinder = π * (r)2 * h

= (22/7) * 5 * 5 * 7

= 550

Area of cylinder = 2 * π * r * (h + r)

= 2 * (22/7) * 5 * (7 + 5)

= 377.1428571428571

Height = 21, Radius = 10

Volume of cylinder = 6600

Area of cylinder = 1948.5714285714284

Explanation

Volume of cylinder = π * (r)2 * h

= (22/7) * 10 * 10 * 21

= 6600

Area of cylinder = 2 * π * r * (h + r)

= 2 * (22/7) * 10 * (21 + 10)

= 1948.5714285714284

Calculating the volume and area of a cylinder: AlgorithmStep 1 − Declare a variable for storing the height of the cylinder- ‘h’.

Step 2 − Declare a variable for storing the radius of the cylinder- ‘r’.

Step 3 − Declare a variable for storing the area of the cylinder- ‘area’ and declare another variable for storing the volume- ‘volume’ and initialize both the variables with value 0.

Step 4 − Calculate the volume using the formula- Volume = π * (r)2 * h, and store it in the ‘volume’ variable in the function calculateVolumeOfCylinder().

Step 5 − Calculate the area using the formula- Area = 2 * π * r * (h + r), and store it in the ‘area’ variable in the function calculateAreaOfCylinder().

Step 6 − Print the calculated volume and area of the cylinder, i.e, the value stored in the ‘volume’ and ‘area’ variables.

Exampleimport

“fmt”

func

calculateVolumeOfCylinder

(

h

,

r float64

)

float64

{

var

volume float64

=

0

volume

=

(

22.0

/

7.0

)

*

r

*

r

*

h

return

volume

}

func

calculateAreaOfCylinder

(

h

,

r float64

)

float64

{

var

area float64

=

0

area

=

2

*

(

22.0

/

7.0

)

*

r

*

(

h

+

r

)

return

area

}

func

main

(

)

{

var

h float64

=

7

var

r float64

=

5

var

area

,

volume float64

fmt

.

Println

(

“Program to calculate the volume and area of the Cylinder n”

)

volume

=

calculateVolumeOfCylinder

(

h

,

r

)

area

=

calculateAreaOfCylinder

(

h

,

r

)

fmt

.

Println

(

“Height of the cylinder : “

,

h

)

fmt

.

Println

(

“Radius of the cylinder : “

,

r

)

fmt

.

Println

(

“Therefore, Volume of cylinder : “

,

volume

)

fmt

.

Println

(

“Area of cylinder : “

,

area

)

}

Output Program to calculate the volume and area of the Cylinder Height of the cylinder : 7 Radius of the cylinder : 5 Therefore, Volume of cylinder : 550 Area of cylinder : 377.1428571428571 Description of code

var h float64 = 7, var r float64 = 5 − In this line, we are declaring the variables for height ‘h’ and radius ‘r’. As the height and radius will be of data type float, that’s why we are using the float64 data type.

calculateVolumeOfCylinder(h, r float64) float64 − This is the function where we are calculating the volume of the cylinder. The function has the variable ‘h’ and ‘r’ of data type float64 as the parameter and also has a return type of float64.

volume = (22.0 / 7.0) * r * r * h − If we desire the output to be of data type float, we explicitly need to convert the values, that is the reason we are using values 22.0 and 7.0 instead of 22 and 7. Using the above formula, we calculate the volume of the cylinder.

return volume − for returning the volume of the cylinder.

volume = calculateVolumeOfCylinder(h, r) − We are calling the function calculateVolumeOfCylinder() and storing the calculated value in the ‘volume’ variable.

calculateAreaOfCylinder(h, r float64) float64 − This is the function where we are calculating the area of the cylinder. The function has the variable ‘h’ and ‘r’ of data type float64 as the parameter and also has a return type of float64.

area = 2 * (22.0 / 7.0) * r * (h + r) − Since we are desiring the output to be of data type float, we explicitly need to convert the values here as well, that is the reason we are using values 22.0 and 7.0 instead of 22 and 7. Using the above formula, we calculate the area of the cylinder.

return area − for returning the area of the cylinder

area = calculateAreaOfCylinder(h, r) − We are calling the function calculateAreaOfCylinder() and storing the calculated value in the ‘area’ variable.

Therefore, Volume of the cylinder = (22/7) * 5 * 5 * 7

= 550

Area of the cylinder = 2 * (22/7) * 5 * (7 + 5)

= 377.1428571428571

ConclusionThis is all about calculating the volume and area of a cylinder using Go programming. We have also maintained code modularity by using separate functions for calculating the area and volume which also increases the code reusability. You can explore more about Golang programming using these tutorials.

## Golang Program To Find Maximum Sum Of A Subarray With Length K

In this article we are going to understand how to use methods naming brute-force, sliding window and prefix sum methods of golang to find the maximum sum of a subarray with length k. We will also discuss the Algorithm for each method and provide code Examples to demonstrate their implementation.

Syntax func len(v Type) intThe len() function is used to get the length of a any parameter. It takes one parameter as the data type variable whose length we wish to find and returns the integer value which is the length of the variable.

Example 1The first Example to find maximum sum of a subarray with length k is Brute-Force Method.

package main import ( "fmt" "math" ) func maxSumBruteForce(arr []int, k int) int { n := len(arr) maxSum := math.MinInt64 for i := 0; i <= n-k; i++ { sum := 0 for j := i; j < i+k; j++ { sum += arr[j] } maxSum = sum } } return maxSum } func main() { x := []int{1, 2, 3, 4, 5} fmt.Println("The given array of integers is:", x) var num int = 5 result := maxSumBruteForce(x, num) fmt.Println("The max sum is:", result) } Output The given array of integers is: [1 2 3 4 5] The max sum is: 15 Example 2In this Example we will write a go language program to find the maximum sum of a subarray having k number of elements by using prefix sum method.

package main import ( "fmt" "math" ) func maxSumPrefixSum(arr []int, k int) int { n := len(arr) maxSum := math.MinInt64 prefixSum := make([]int, n+1) for i := 1; i <= n; i++ { prefixSum[i] = prefixSum[i-1] + arr[i-1] } for i := k; i <= n; i++ { sum := prefixSum[i] - prefixSum[i-k] maxSum = sum } } return maxSum } func main() { x := []int{1, 2, 3, 4, 5, 6} fmt.Println("The given array of integers is:", x) var num int = 6 result := maxSumPrefixSum(x, num) fmt.Println("The max sum is:", result) } Output The given array of integers is: [1 2 3 4 5 6] The max sum is: 21 ConclusionIn this article, we explored three different methods to find the maximum sum of a subarray of a given length k in Golang. Here we have used two methods viz. The brute-force method and prefix sum method. The brute force method has a time complexity of O(nk), while prefix sum methods have a time complexity of O(n). The prefix sum methods is more efficient than the brute-force method, as it avoids unnecessary calculations.

## Golang Program To Find The Current Working Directory

In go language we can use the functions present in os package like Getwd() and Args to find the current directory in which our code is getting executed.

The directory from which the program is currently being run is called the current directory. It is also known as present working directory.

The directory in which the program is currently running is called the working directory. It is the parent directory for any files or the or directory that are created during runtime.

Algorithm

First, we need to import the “fmt” and “os” packages.

Then, start the main() function. Inside the main() call the required method present in the respective package.

Check if there is an error by checking if “err” is not nil

If there is an error, print the error and stop the further execution of the program.

If there is no error, print the current working directory, which is stored in the “dir” variable.

Syntax funcGetwd() (dir string, err error)The Getwd() function is present in os package and is used to get the rooted path of a particular directory. The function returns two values. One is the string variable containing the path length of directory and other is the error message. The error is not null if there is some problem in getting the required result.

funcDir(path string) stringThe Dir() function is present in the filepath package and is used to return all the elements of the specified path except the last element. The function accepts one argument which is the path length of the directory and returns all the elements of the specified path except the last element.

funcAbs(path string) (string, error)The Abs() function is present in path package and is used to return an absolute representation of specified path. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. The function accepts the specified path as an argument and returns the absolute representation of the specified path along with the error.

Example 1In this Example we will write a go language program to find the current working directory by using the Getwd() function present in os package.

package main import ( "fmt" "os" ) func main() { dir, err := os.Getwd() if err != nil { fmt.Println(err) return } fmt.Println("Current working directory:", dir) } Output Current working directory: C:UsersLENOVODesktopgo Example 2Another way to find the current working directory in Golang is by using the os.Args[0] value. This value represents the name of the executable program. By using this value, we can find the current working directory of the program by removing the program name from the full path of the program.

package main import ( "fmt" "os" "path/filepath" ) func main() { dir := filepath.Dir(os.Args[0]) fmt.Println("Current working directory:", dir) } Output Current working directory: C:UsersLENOVOAppDataLocalTempgo-build3596082773b001exe Example 3We can also use other methods to find the current working directory in Golang by using the chúng tôi and chúng tôi functions from the path/filepath package. The chúng tôi function returns the absolute path of a file, and the chúng tôi function returns the directory of a file. We can use these two functions together to find the current working directory of the Go program.

package main import ( "fmt" "path/filepath" "os" ) func main() { dir, err := filepath.Abs(filepath.Dir(os.Args[0])) if err != nil { fmt.Println(err) return } fmt.Println("Current working directory:", dir) } Output Current directory: C:UsersLENOVOAppDataLocalTempgo-build514274184b001exe Conclusion## How To Find Area And Perimeter: 11 Steps (With Pictures)

This article was co-authored by David Jia . David Jia is an Academic Tutor and the Founder of LA Math Tutoring, a private tutoring company based in Los Angeles, California. With over 10 years of teaching experience, David works with students of all ages and grades in various subjects, as well as college admissions counseling and test preparation for the SAT, ACT, ISEE, and more. After attaining a perfect 800 math score and a 690 English score on the SAT, David was awarded the Dickinson Scholarship from the University of Miami, where he graduated with a Bachelor’s degree in Business Administration. Additionally, David has worked as an instructor for online videos for textbook companies such as Larson Texts, Big Ideas Learning, and Big Ideas Math. This article has been viewed 301,141 times.

Article Summary

X

Finding the area and perimeter of a shape can be extremely useful not only in math, but for household projects, construction, and DIY projects. The perimeter is the length of the entire outside boundary of a polygon, and the area is the measure of the space that fills the boundaries of a polygon.

To measure the perimeter of a rectangle, you will want to measure each side of the rectangle. We can use a rectangle on a piece of paper as an example. Find the length of one side of your rectangle. You can do this with a ruler, measuring tape, or by making up your own example. Write down this number by the side it represents so you don’t forget its length. In this example, we found the length of the rectangle to be 3 feet or 92 centimeters. Then, find the width of one side of your rectangle. Write down the value for your width next to the horizontal side of your rectangle it represents. In this example, we found the length of the rectangle to be 5 feet or 152 centimeters. Write down the correct measurements on the opposing sides of your rectangle. Rectangles have four sides, but the length of opposite sides will be the same.

Add all your sides together. One a piece of scratch paper, or on the paper you have written the guided example, write: length + length + width + width. So, for the guided example, you would add 3 + 3 + 5 + 5 to get a perimeter of 16 feet (4.9 m). Here are the formulas for other common shapes: Square: length of any side x 4 Triangle: side 1 + side 2 + side 3 Irregular polygon: add all sides Circle: 2 x π x radius OR π x diameter

To find the area of a rectangle, you will start by finding the dimensions of your shape. We will use the rectangle from the previous example. This rectangle is 3 feet (92 cm) by 5 feet (152 cm). Multiply the length of your rectangle by the width to get the area. The final answer will be written in square units. You will want to change your formula according to shape. Unfortunately, different geometric shapes will require you to take a different approach to solve for area. You can use the following formulas for finding the area of some common shapes: Parallelogram: base x height Square: side 1 x side 2 Triangle: ½ x base x height. Circle: π x radius²

That is how you can find the area and perimeter of common shapes. Keep reading to learn the formulas for finding the area of different shapes!

Did this summary help you?

## Golang Program To Find The Distinct Elements From Two Arrays

In this tutorial, we will see to write a go language program to find distinct elements in two arrays. In this article we will write two programs. In the first program we will use the array of strings while in the second one we will use the array of integers.

AlgorithmSTEP 1 − import the fmt package.

STEP 2 − Define three functions named intersection(), uniquearr1 and uniquearr2.

STEP 3 − The intersection() finds the common array elements from the two arrays while the other two functions remove that common elements from given arrays.

STEP 4 − All these functions use for loop to iterate over the two arrays and check if the current element of one array is equal to the elements of the other.

STEP 5 − Start the main() function.

STEP 6 − Initialize two arrays of strings and store values to them.

STEP 7 − Call the intersection() function and store the final result in a different variable.

STEP 8 − Now, call the uniquearr1() and uniquearr2() by passing the arrays and the result array as arguments to it. store the result and append the two arrays in a new array.

STEP 9 − Print the result on the screen.

Example 1The following code illustrates how we can find the distinct elements in two different arrays of strings.

package main import "fmt" func intersection(arr1, arr2 []string) []string { out := []string{} bucket := map[string]bool{} for _, i := range arr1 { for _, j := range arr2 { if i == j && !bucket[i] { out = append(out, i) bucket[i] = true } } } return out } func uniquearr1(arr1, result []string) []string { index := len(arr1) index1 := len(result) for i := 0; i <= index-1; i++ { for j := 0; j <= index1-1; j++ { if arr1[i] == result[j] { arr1[i] = arr1[index-1] arr1[index-1] = "" arr1 = arr1[:index-1] index = index - 1 i = 0 } } } return arr1 } func uniquearr2(arr2, result []string) []string { index1 := len(result) lenarr2 := len(arr2) for i := 0; i <= lenarr2-1; i++ { for j := 0; j <= index1-1; j++ { if arr2[i] == result[j] { arr2[i] = arr2[lenarr2-1] arr2[lenarr2-1] = "" arr2 = arr2[:lenarr2-1] lenarr2 = lenarr2 - 1 i = 0 } } } return arr2 } func main() { arr1 := []string{"apple", "mango", "banana", "papaya"} fmt.Println("The first array entered is:", arr1) arr2 := []string{"cherry", "papaya", "mango"} fmt.Println("The second array entered is:", arr2) result := intersection(arr1, arr2) fmt.Println() result1 := uniquearr1(arr1, result) result2 := uniquearr2(arr2, result) var finalres []string finalres = append(finalres, result1...) finalres = append(finalres, result2...) fmt.Println("The final array containing distinct values from the above mentioned arrays is:", finalres) } Output The first array entered is: [apple mango banana papaya] The second array entered is: [cherry papaya mango] The final array containing distinct values from the above mentioned arrays is: [apple banana cherry] Example 2The following code illustrates how we can find the distinct elements in two different arrays of integers in go programming language.

package main import "fmt" func intersection(arr1, arr2 []int) []int { out := []int{} bucket := map[int]bool{} for _, i := range arr1 { for _, j := range arr2 { if i == j && !bucket[i] { out = append(out, i) bucket[i] = true } } } return out } func uniquearr1(arr1, result []int) []int { index := len(arr1) index1 := len(result) for i := 0; i <= index-1; i++ { for j := 0; j <= index1-1; j++ { if arr1[i] == result[j] { arr1[i] = arr1[index-1] arr1[index-1] = 0 arr1 = arr1[:index-1] index = index - 1 i = 0 } } } return arr1 } func uniquearr2(arr2, result []int) []int { index1 := len(result) lenarr2 := len(arr2) for i := 0; i <= lenarr2-1; i++ { for j := 0; j <= index1-1; j++ { if arr2[i] == result[j] { arr2[i] = arr2[lenarr2-1] arr2[lenarr2-1] = 0 arr2 = arr2[:lenarr2-1] lenarr2 = lenarr2 - 1 i = 0 } } } return arr2 } func main() { arr1 := []int{11, 25, 35, 23, 54} fmt.Println("The first array entered is:", arr1) arr2 := []int{35, 89, 60, 54, 23} fmt.Println("The second array entered is:", arr2) result := intersection(arr1, arr2) fmt.Println() result1 := uniquearr1(arr1, result) result2 := uniquearr2(arr2, result) var finalres []int finalres = append(finalres, result1...) finalres = append(finalres, result2...) fmt.Println("The final array containing distinct values from the above mentioned arrays is:", finalres) } Output The first array entered is: [11 25 35 23 54] The second array entered is: [35 89 60 54 23] The final array containing distinct values from the above mentioned arrays is: [11 25 60 89] ConclusionWe have successfully compiled and executed a go language program to find the distinct elements of two arrays along with the examples.

Update the detailed information about **How To Find The Area Of A Circle In Golang?** on the Tai-facebook.edu.vn website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!