- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the Perimeter of a Rectangle in Golang?
In this tutorial, we are going to see the Golang program to find the perimeter of a rectangle. Perimeter is the total length of the boundary of any closed figure
Formula
Perimeter of rectangle - 2 * ( L + B ) L - Length of a rectangle B - Breadth of a rectangle
For example, the length of a rectangle is 100 cm and the breadth is 50 cm so the Perimeter of a rectangle is −
Perimeter = 2 * (100 + 50) cm = 2 * (150) cm = 300 cm
Finding the Perimeter of a circle within the function
Algorithm
STEP 1 − Declaring the variables for the length, breadth, and the parameter of the float64 data type.
STEP 2 − Taking the input for the length and breadth from the user.
STEP 3 − Finding the perimeter 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 is the input the program will take sane 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 Perimeter of a rectangle 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 length and breadth also a variable to store Perimeter var length, breadth, Perimeter float64 fmt.Println("Program to find the perimeter of a rectangle.") // initializing the length of a rectangle length = 30 // initializing the breadth of a rectangle breadth = 10.4 // finding the Perimeter of a rectangle Perimeter = 2.0 * (length + breadth) // printing the result fmt.Println("Length = ", length, "\nBreath =", breadth, "\nPreimeter of the Rectangle = ", Perimeter, "cm.") fmt.Println("(Finding the Perimeter of a circle within the function)") }
Output
Program to find the perimeter of a rectangle. Length = 30 Breath = 10.4 Preimeter of the Rectangle = 80.8 cm. (Finding the Perimeter of a circle within the function)
Description of code
var length, breadth, Perimeter float64 − In this line, we are declaring the length, breadth, and perimeter that we are going to use later. As the length, breadth, or perimeter can be in decimal so we have used the float data type.
Perimeter = 2.0 * (length + breadth) − In this line of code, we are applying the formula and finding the perimeter.
fmt.Println("The perimeter of a rectangle whose length is", length, "and the breadth is", breadth, "is", Perimeter, "cm.") − Printing the perimeter of a rectangle.
Finding the perimeter of a circle in different function
Algorithm
STEP 1 − Declaring the variables for the length, breadth, and the parameter of float64 data type.
STEP 2 − Taking the input for the length and breadth from the user.
STEP 3 − Calling the function with the length and breadth as a parameter, and storing the perimeter the function is returning
STEP 4 − Printing the result.
Example 2
In this example, we are going to find the Perimeter of a rectangle by defining the separate function to find the perimeter
package main // fmt package provides the function to print anything import ( "fmt" ) func perimeterOfRectangle(length, breadth float64) float64 { // returning the perimeter of a rectangle using the formula return 2 * (length + breadth) } func main() { // declaring the floating variables using the var keyword for // storing the length and breadth also a variable to store parameter var length, breadth, perimeter float64 fmt.Println("Program to find the perimeter of a rectangle.") // taking the length of a rectangle as input from the user fmt.Print("Please enter the length of a rectangle:") fmt.Scanln(&length) // taking the breadth of a rectangle as input from the user fmt.Print("Please enter the breadth of a rectangle: ") fmt.Scanln(&breadth) // calling the perimeter of rectangle function by passing the respective parameter // and storing the result perimeter = perimeterOfRectangle(length, breadth) // printing the result fmt.Println("The perimeter of a rectangle whose length is", length, "and the breadth is", breadth, "is", perimeter, "cm.") fmt.Println("(Finding the perimeter of a circle in different function)") }
Output
Program to find the perimeter of a rectangle. Please enter the length of a rectangle:20.5 Please enter the breadth of a rectangle: 10 The parameter of a rectangle whose length is 20.5 and the breadth is 10 is 61 cm. (Finding the perimeter of a circle in different function)
Description of code:
var length, breadth, Perimeter float64 − In this line, we are declaring the length, breadth, and perimeter that we are going to use later. As the length, breadth, or perimeter can be in decimal so we have used the float data type.
fmt.Scanln(&length) − Taking the input for the length from the user
fmt.Scanln(&breadth) − Taking the input for the breadth from the user.
perimeter = perimeterOfRectangle(length, breadth) − In this line of code, we are calling the function that is finding the perimeter of a rectangle.
fmt.Println("The perimeter of a rectangle whose length is", length, "and the breadth is", breadth, "is", Perimeter, "cm.") − Printing the perimeter of a rectangle.
Conclusion
These are the two ways to find the perimeter of a rectangle 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.
- Related Articles
- How to Find the Perimeter of a Circle in Golang?
- Java Program to Find the Perimeter of a Rectangle
- Swift Program to Find the Perimeter of a Rectangle
- Kotlin Program to Find the Perimeter of a Rectangle
- Golang program to find the area of a rectangle
- JavaScript program to find Area and Perimeter of Rectangle
- Golang Program to Find the Area of a Rectangle Using Classes
- Program to find Perimeter / Circumference of Square and Rectangle in C++
- The length of a rectangle is 50 cm if the perimeter of the rectangle is 150 cm. Find the area of the rectangle.
- The length of rectangle is $25\ m$ and width of rectangle is $56\ cm$ find the perimeter of rectangle.
- Find the Perimeter of Triangle ABC.Find the Perimeter of Rectangle BCDE. from the following figure."\n
- A rectangle has a length 15 cm and the perimeter is 50 cm. Find the breadth of the rectangle.
- The length of a rectangle is 16 cm. If the perimeter of the rectangle is 60 cm then find the breadth of the rectangle.
- The perimeter of a rectangle is $130\ cm$. If the breadth of the rectangle is $30\ cm$, find its length. Also find the area of the rectangle.
- The breadth of a rectangle is equal to its length. If the perimeter of the rectangle is $88\ cm$, find its dimensions.
