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.

<div class="execute"></div><div class="code-mirror  language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>

<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
   <span class="token string">"fmt"</span>
<span class="token punctuation">)</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

   <span class="token comment">// declaring the floating variables using the var keyword for</span>
   <span class="token comment">// storing the length and breadth also a variable to store Perimeter</span>
   <span class="token keyword">var</span> length<span class="token punctuation">,</span> breadth<span class="token punctuation">,</span> <span class="token class-name">Perimeter</span> float64
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Program to find the perimeter of a rectangle."</span><span class="token punctuation">)</span>
   
   <span class="token comment">// initializing the length of a rectangle</span>
   length <span class="token operator">=</span> <span class="token number">30</span>
   
   <span class="token comment">// initializing the breadth of a rectangle</span>
   breadth <span class="token operator">=</span> <span class="token number">10.4</span>
   
   <span class="token comment">// finding the Perimeter of a rectangle</span>
   <span class="token class-name">Perimeter</span> <span class="token operator">=</span> <span class="token number">2.0</span> <span class="token operator">*</span> <span class="token punctuation">(</span>length <span class="token operator">+</span> breadth<span class="token punctuation">)</span>
   
   <span class="token comment">// printing the result</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Length = "</span><span class="token punctuation">,</span> length<span class="token punctuation">,</span> <span class="token string">"\nBreath ="</span><span class="token punctuation">,</span> breadth<span class="token punctuation">,</span> <span class="token string">"\nPreimeter of the Rectangle = "</span><span class="token punctuation">,</span> <span class="token class-name">Perimeter</span><span class="token punctuation">,</span> <span class="token string">"cm."</span><span class="token punctuation">)</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"(Finding the Perimeter of a circle within the function)"</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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

<div class="code-mirror  language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>

<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
   <span class="token string">"fmt"</span>
<span class="token punctuation">)</span>
func <span class="token function">perimeterOfRectangle</span><span class="token punctuation">(</span>length<span class="token punctuation">,</span> breadth float64<span class="token punctuation">)</span> float64 <span class="token punctuation">{</span>
   <span class="token comment">// returning the perimeter of a rectangle using the formula</span>
   <span class="token keyword">return</span> <span class="token number">2</span> <span class="token operator">*</span> <span class="token punctuation">(</span>length <span class="token operator">+</span> breadth<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

   <span class="token comment">// declaring the floating variables using the var keyword for</span>
   <span class="token comment">// storing the length and breadth also a variable to store parameter</span>
   <span class="token keyword">var</span> length<span class="token punctuation">,</span> breadth<span class="token punctuation">,</span> perimeter float64
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Program to find the perimeter of a rectangle."</span><span class="token punctuation">)</span>
   
   <span class="token comment">// taking the length of a rectangle as input from the user</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Print</span><span class="token punctuation">(</span><span class="token string">"Please enter the length of a rectangle:"</span><span class="token punctuation">)</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Scanln</span><span class="token punctuation">(</span><span class="token operator">&</span>length<span class="token punctuation">)</span>
   
   <span class="token comment">// taking the breadth of a rectangle as input from the user</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Print</span><span class="token punctuation">(</span><span class="token string">"Please enter the breadth of a rectangle: "</span><span class="token punctuation">)</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Scanln</span><span class="token punctuation">(</span><span class="token operator">&</span>breadth<span class="token punctuation">)</span>
   
   <span class="token comment">// calling the perimeter of rectangle function by passing the respective parameter</span>
   <span class="token comment">// and storing the result</span>
   perimeter <span class="token operator">=</span> <span class="token function">perimeterOfRectangle</span><span class="token punctuation">(</span>length<span class="token punctuation">,</span> breadth<span class="token punctuation">)</span>
   
   <span class="token comment">// printing the result</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"The perimeter of a rectangle whose length is"</span><span class="token punctuation">,</span> length<span class="token punctuation">,</span> <span class="token string">"and the breadth is"</span><span class="token punctuation">,</span> breadth<span class="token punctuation">,</span> <span class="token string">"is"</span><span class="token punctuation">,</span>    perimeter<span class="token punctuation">,</span> <span class="token string">"cm."</span><span class="token punctuation">)</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"(Finding the perimeter of a circle in different function)"</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div>

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.

Updated on: 2022-08-29T07:46:18+05:30

443 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements