Swift Program to Calculate the Power of a Number

This tutorial will discuss how to write a swift program to calculate the power of a number.

Power of a number means how many times a number is used in multiplication, for example, 16 ^2 = 16 * 16 = 256 or 16 to the power of 2. It is also known as exponent or indices.

Below is a demonstration of the same ?

Suppose our given input is ?

Number - 16

Exponent Value is - 3

The desired output is ?

Final result is 16^3 = 4096

Calculating power of a number using recursion

We can calculate the power of a number using recursion. Recursion is a process in which a function calls itself to solve a problem.

Algorithm

Following are the algorithm steps to calculate the power of a number using recursion ?

  • Step 1 ? Create a recursive function. This function returns the power of the given number.

  • Step 2 ? Call the recursive function with two parameters that are the base number and the exponent number.

  • Step 3 ? Display the final output.

Example

The following program shows how to calculate the power of a number using recursion ?

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

func <span class="token function">powerRecursion</span><span class="token punctuation">(</span>baseNumber<span class="token operator">:</span> Int<span class="token punctuation">,</span> exponentNumber<span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> Int<span class="token punctuation">{</span>

   <span class="token comment">// Declare a variable of int type it is used to store the power of the number</span>
   <span class="token comment">// Here the initial value of the variable is 1</span>
   <span class="token keyword">var</span> PowerOfNumber <span class="token operator">:</span> Int <span class="token operator">=</span> <span class="token number">1</span>

   <span class="token comment">// Checking if the exponent is greater than 0</span>
   <span class="token keyword">if</span> exponentNumber <span class="token operator">></span> <span class="token number">0</span> <span class="token punctuation">{</span>

      <span class="token comment">// Calculating the power of the number</span>

      PowerOfNumber <span class="token operator">=</span> baseNumber <span class="token operator">*</span> <span class="token punctuation">(</span><span class="token function">powerRecursion</span><span class="token punctuation">(</span>baseNumber<span class="token operator">:</span> baseNumber<span class="token punctuation">,</span> exponentNumber<span class="token operator">:</span> exponentNumber<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
   <span class="token punctuation">}</span>
   <span class="token keyword">return</span> PowerOfNumber
<span class="token punctuation">}</span>
<span class="token keyword">var</span> finalResult <span class="token operator">=</span> <span class="token function">powerRecursion</span><span class="token punctuation">(</span>baseNumber<span class="token operator">:</span> <span class="token number">18</span><span class="token punctuation">,</span> exponentNumber<span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Power is - "</span><span class="token punctuation">,</span> finalResult<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

Power is - 5832

In the above code, we create a recursive function named powerRecursion() to calculate the power of a number by calling itself recursively. After creating this function, we called the function using two parameters that are 13 and 8 and display the final result which is 5832.

Calculating power of a number using pow() function

We can calculate the power of a number using the pow() function. It is used to calculate the power of a number. It takes two arguments and returns the power of the number.

Syntax

pow(baseNumber, exponentNumber)

Example

The following program shows how to calculate the power of a number using the pow() function with user defined input.

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

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Please enter the number"</span><span class="token punctuation">)</span>

<span class="token comment">// Here we use Double() function to convert the input into double.</span>
<span class="token keyword">var</span> baseNumber <span class="token operator">=</span> <span class="token function">Double</span><span class="token punctuation">(</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">!</span><span class="token punctuation">)</span><span class="token operator">!</span>

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Please enter the exponent"</span><span class="token punctuation">)</span>

<span class="token comment">// Here we use Double() function to convert the input into double.</span>
<span class="token keyword">var</span> exponentNumber <span class="token operator">=</span> <span class="token function">Double</span><span class="token punctuation">(</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">!</span><span class="token punctuation">)</span><span class="token operator">!</span>

<span class="token keyword">var</span> PowerOfNumber <span class="token operator">=</span> <span class="token function">pow</span><span class="token punctuation">(</span>baseNumber<span class="token punctuation">,</span> exponentNumber<span class="token punctuation">)</span>

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Entered Number is - "</span><span class="token punctuation">,</span> baseNumber<span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Entered exponent is - "</span><span class="token punctuation">,</span> exponentNumber<span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"\n Power of the number is - "</span><span class="token punctuation">,</span> PowerOfNumber<span class="token punctuation">)</span>
</div>

STDIN Input

Entered Number is - 18.0 
Entered exponent is - 3.0

Output

Power of the number is - 5832.0

In the above code, we calculate the power of a number using the pow() function. Here the number is entered by the user using the readLine() function and the entered number is further converted into double using Double() function because pow() function return values in double.

Updated on: 2022-08-01T09:26:18+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements