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 Area of Square in Golang?
In this tutorial, we are going to see the Golang program to find the Area of a Square. The area is the total space covered by any closed figure.

Formula
Area of Square - (length of a side) * (length of a side) s - length of a side of a Square
For example, the length of a side of a Square is 10 cm so the Area of a Square is ?
Area = 10 * 10
= 100 cm^2
Finding the Area of a Square within the function
Algorithm
Step 1 ? Declaring the variables for the length of a side 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 Square 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 side of the Square also a variable area</span>
<span class="token comment">// to store Area</span>
<span class="token keyword">var</span> lengthOfSide<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 Square."</span><span class="token punctuation">)</span>
<span class="token comment">// initializing the length of the side of a Square</span>
lengthOfSide <span class="token operator">=</span> <span class="token number">10</span>
<span class="token comment">// finding the Area of a Square</span>
<span class="token class-name">Area</span> <span class="token operator">=</span> <span class="token punctuation">(</span>lengthOfSide <span class="token operator">*</span> lengthOfSide<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 Square whose length of the side is"</span><span class="token punctuation">,</span> lengthOfSide<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 Square 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 Square. The Area of a Square whose length of the side is 10 is 100 cm * cm. (Finding the Area of a Square within the function)
Description of code
var lengthOfSide, Area float64 ? In this line, we are declaring the length of a side and the Area that we are going to use later. As the length of a side or Area can be in decimal, we have used the float data type.
Area = (lengthOfSide * lengthOfSide) ? In this line of code, we are applying the formula and finding the Area.
-
fmt.Println("The Area of a Square whose length of side is", lengthOfSide, "is", Area, "cm * cm.")
? Printing the Area of a Square.
Finding the Area of a Square in the separate function
Algorithm
Step 1 ? Declaring the variables for the length of a side and the area of the float64 data type.
Step 2 ? Initialize the variable.
Step 3 ? Calling the function with the length of a side 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 Square 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>
func <span class="token function">areaOfSquare</span><span class="token punctuation">(</span>lengthOfSide 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> <span class="token punctuation">(</span>lengthOfSide <span class="token operator">*</span> lengthOfSide<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 of the side of the Square also a variable area</span>
<span class="token comment">// to store Area</span>
<span class="token keyword">var</span> lengthOfSide<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 Square."</span><span class="token punctuation">)</span>
<span class="token comment">// initializing the length of the side of a Square</span>
lengthOfSide <span class="token operator">=</span> <span class="token number">10</span>
<span class="token comment">// finding the Area of a Square by calling areaOfSquare() function</span>
<span class="token class-name">Area</span> <span class="token operator">=</span> <span class="token function">areaOfSquare</span><span class="token punctuation">(</span>lengthOfSide<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 Square whose length of a side is"</span><span class="token punctuation">,</span> lengthOfSide<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 Square 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 Square. The Area of a Square whose length of a side is 10 is 100 cm * cm. (Finding the Area of a Square in the separate function)
Description of code
var lengthOfSide, Area float64 ? In this line, we are declaring the length of a side and the Area that we are going to use later. As the length of a side, or Area can be in decimal so we have used the float data type.
Area = areaOfSquare(lengthOfSide) ? In this line of code, we call the function finding the Area of a Square.
-
fmt.Println("The Area of a Square whose length of side is", lengthOfSide, "is", Area, "cm * cm.") ?
Printing the Area of a Square.
Conclusion
These are the two ways to find the Area of a Square 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.
