Kotlin Program to Find Area of Square

In this article, we will understand how to find the Area of a Square. The area of a square is calculated using the formula.

side*side

Below is a demonstration of the same ?

Suppose our input is ?

Length of the side : 4

The desired output would be ?

Area of the square : 16

Algorithm

  • Step 1 ? START.

  • Step 2 ? Declare 2 integer values namely sideValue and myResult.

  • Step 3 ? Define the values.

  • Step 4 ? Calculate the area of the square using the formula side*side and store the result.

  • Step 5 ? Display the result.

  • Step 6 ? Stop.

Example 1

In this example, we will find the Area of Square using its? sides as shown in the above formula. First, declare and initialize a variable for the side.

val sideValue = 4

Then, find the Area of Square ?

val myResult = sideValue * sideValue

Let us now see the complete example to find the Area of Square ?

<div class="execute"></div><div class="code-mirror  language-kotlin" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">fun</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token keyword">val</span> sideValue <span class="token operator">=</span> <span class="token number">4</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The length of side of the square is defined as <span class="token interpolation variable">$sideValue</span>"</span><span class="token punctuation">)</span>
   <span class="token keyword">val</span> myResult <span class="token operator">=</span> sideValue <span class="token operator">*</span> sideValue
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The area of square is: <span class="token interpolation variable">$myResult</span>"</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

The length of side of the square is defined as 4
The area of square is: 16

Example 2

In this example, we will find the Area of Square.

<div class="execute"></div><div class="code-mirror  language-kotlin" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">fun</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token keyword">val</span> sideValue <span class="token operator">=</span> <span class="token number">4</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The length of side of the square is defined as <span class="token interpolation variable">$sideValue</span>"</span><span class="token punctuation">)</span>
   <span class="token function">squareArea</span><span class="token punctuation">(</span>sideValue<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token keyword">fun</span> <span class="token function">squareArea</span><span class="token punctuation">(</span>sideValue<span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token keyword">val</span> myResult <span class="token operator">=</span> sideValue <span class="token operator">*</span> sideValue
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The area of square is: <span class="token interpolation variable">$myResult</span>"</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

The length of side of the square is defined as 4
The area of square is: 16
Updated on: 2022-10-17T08:36:25+05:30

848 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements