Kotlin Program to Find the Perimeter of a Circle

In this article, we will understand how to find the perimeter of a circle. The perimeter of a circle is calculated using the formula.

(2*22*radius)/7

You can also write the above as ?

2?r

The value of ? is 22/7 or 3.14.

Suppose our input is ?

Radius of the circle : 5

The desired output would be ?

Perimeter of Circle is: 31.428571428571427

Algorithm

  • Step 1 ? START.

  • Step 2 ? Declare 2 double values namely radius and myResult.

  • Step 3 ? Define the values.

  • Step 4 ? Calculate the perimeter using the formula using the formula (2*22*radius)/7 and store the result.

  • Step 5 ? Display the result.

  • Step 6 ? Stop.

Example 1

In this example, we will find the Perimeter of a Circle. First, declare and initialize a variable for the radius of the Circle ?

val radius = 5

Now, in a new variable myResult, calculate the Perimeter using the above formulae ?

val myResult = (2 * 22 * radius)/7

Let us now see the compete example to find the perimeter of the circle ?

<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> radius <span class="token operator">=</span> <span class="token number">5</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The radius of the circle is defined as <span class="token interpolation variable">$radius</span>"</span><span class="token punctuation">)</span>
   <span class="token keyword">val</span> myResult <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">2</span> <span class="token operator">*</span> <span class="token number">22</span> <span class="token operator">*</span> radius<span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">7</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The perimeter of the circle 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 radius of the circle is defined as 5
The perimeter of the circle is: 31

Example 2

In this example, we will find the Perimeter of a Circle.

<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> radius <span class="token operator">=</span> <span class="token number">5</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The radius of the circle is defined as <span class="token interpolation variable">$radius</span>"</span><span class="token punctuation">)</span>
   <span class="token function">circlePerimeter</span><span class="token punctuation">(</span>radius<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token keyword">fun</span> <span class="token function">circlePerimeter</span><span class="token punctuation">(</span>radius<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> <span class="token punctuation">(</span><span class="token number">2</span> <span class="token operator">*</span> <span class="token number">22</span> <span class="token operator">*</span> radius<span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">7</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The perimeter of the circle 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 radius of the circle is defined as 5
The perimeter of the circle is: 31
Updated on: 2022-10-17T08:53:40+05:30

569 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements