Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How To Find The Area of a Parallelogram in Golang?
In this tutorial, we are going to see the Golang program to find the Area of a Parallelogram. The area is the total space covered by any closed figure.

Formula
Area of the Parallelogram - base * height b - length of the base of a Parallelogram h - length of the height of a Parallelogram
For example, the length of the base of a Parallelogram is 6 cm and the length of the height of the parallelogram is 4 cm so the Area of a Parallelogram is ?
b = 6 cm h = 4 cm Area = base * height = 6 * 4 = 24 cm^2
Finding the Area of a Parallelogram within the function
Algorithm
Step 1 ? Declaring the variables for the length of the base, length of height, and the area of the float64 data type.
Step 2 ? Initialize the variable.
Step 3 ? Find the Area using the above formula within the function.
Step 4 ? Printing the result.
Time Complexity: O(1) Space Complexity: O(1)
Example 1
In this example, we are going to find the Area of a Parallelogram 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 of the base of the Parallelogram,</span>
<span class="token comment">// length of the height of the Parallelogram and also</span>
<span class="token comment">// a variable area to store Area</span>
<span class="token keyword">var</span> base<span class="token punctuation">,</span> height<span class="token punctuation">,</span> <span class="token class-name">Area</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 Area of a Parallelogram."</span><span class="token punctuation">)</span>
<span class="token comment">// initializing the length of the base of a Parallelogram</span>
base <span class="token operator">=</span> <span class="token number">6</span>
<span class="token comment">// initializing the length of the height of a Parallelogram</span>
height <span class="token operator">=</span> <span class="token number">4</span>
<span class="token comment">// finding the Area of a Parallelogram</span>
<span class="token class-name">Area</span> <span class="token operator">=</span> height <span class="token operator">*</span> base
<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 Area of a Parallelogram with base ="</span><span class="token punctuation">,</span> base<span class="token punctuation">,</span><span class="token string">"and height ="</span><span class="token punctuation">,</span> height<span class="token punctuation">,</span> <span class="token string">"is"</span><span class="token punctuation">,</span> <span class="token class-name">Area</span><span class="token punctuation">,</span> <span class="token string">"cm * 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 Area of a Parallelogram 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 Area of a Parallelogram. The Area of a Parallelogram with base = 6 and height = 4 is 24 cm * cm. (Finding the Area of a Parallelogram within the function)
Finding the Area of a Parallelogram in the separate function
Algorithm
Step 1 ? Declaring the variables for the length of the base, length of height, and the area of the float64 data type.
Step 2 ? Initialize the variable.
Step 3 ? Calling the function with the length of the base of the Parallelogram and the length of the height of the Parallelogram, 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 Parallelogram by defining the separate function to find the Area.
<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>
<span class="token comment">// in this line we have declared the function that have float64</span>
<span class="token comment">// type parameter and float64 type returntype</span>
func <span class="token function">areaOfParallelogram</span><span class="token punctuation">(</span>base<span class="token punctuation">,</span> height float64<span class="token punctuation">)</span> float64 <span class="token punctuation">{</span>
<span class="token comment">// returning the area by applying the formula</span>
<span class="token keyword">return</span> base <span class="token operator">*</span> height
<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 of the base of the Parallelogram,</span>
<span class="token comment">// length of the height of the Parallelogram and also</span>
<span class="token comment">// a variable area to store Area</span>
<span class="token keyword">var</span> base<span class="token punctuation">,</span> height<span class="token punctuation">,</span> <span class="token class-name">Area</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 Area of a Parallelogram."</span><span class="token punctuation">)</span>
<span class="token comment">// initializing the length of the base of a Parallelogram</span>
base <span class="token operator">=</span> <span class="token number">6</span>
<span class="token comment">// initializing the length of the height of a Parallelogram</span>
height <span class="token operator">=</span> <span class="token number">4</span>
<span class="token comment">// finding the Area of a Parallelogram by calling the</span>
<span class="token comment">// function with the respective parameters</span>
<span class="token class-name">Area</span> <span class="token operator">=</span> <span class="token function">areaOfParallelogram</span><span class="token punctuation">(</span>base<span class="token punctuation">,</span> height<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 Area of a Parallelogram with base ="</span><span class="token punctuation">,</span> base<span class="token punctuation">,</span><span class="token string">"and height ="</span><span class="token punctuation">,</span> height<span class="token punctuation">,</span> <span class="token string">"is"</span><span class="token punctuation">,</span> <span class="token class-name">Area</span><span class="token punctuation">,</span> <span class="token string">"cm * 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 Area of a Parallelogram in the separate 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 Area of a Parallelogram. The Area of a Parallelogram with base = 6 and height = 4 is 24 cm * cm. (Finding the Area of a Parallelogram in the separate function)
Conclusion
These are the two ways to find the Area of a Parallelogram 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.
