Golang Program to Calculate The Power using Recursion

In this tutorial, we will learn how to calculate the power using recursion technique in Go programming language.

Power can be defined as a number being multiplied by itself a specific number of times.

Exponent can be defined to the number of times a number is used in a multiplication. Powers and exponents are important tools to rewrite long multiplication problems in mathematics, especially in algebra.

Example: 24 = 2 × 2 × 2 × 2 = 16, where 2 is the base and 4 is the exponent.

A Recursion is where a function calls itself by direct or indirect means. Every recursive function has a base case or base condition which is the final executable statement in recursion and halts further calls.

Below we have shown examples with two different kinds of recursion methods.

Example 1: Golang Program Code to calculate the power using Direct Recursion Method

Syntax

Result = (num * POWER(num, power-1)
// Recursive function call to the function POWER() by itself up to the defined condition

Algorithm

  • Step 1 ? Import the package fmt.

  • Step 2 ? Create the function POWER().

  • Step 3 ? We will use an if-conditional statement.

  • Step 4 ? Recursive call to the function itself.

  • Step 5 ? Start the function main().

  • Step 6 ? Declare and initialize the variables.

  • Step 7 ? Call the function POWER ().

  • Step 8 ? Print the result on the screen using fmt.Printf().

Example

<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 comment">// GOLANG PROGRAM TO CALCULATE THE POWER USING RECURSION</span>
<span class="token comment">// Direct Recursion example</span>
<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 string">"fmt"</span>

<span class="token comment">// create a function</span>
func <span class="token function">POWER</span><span class="token punctuation">(</span>num <span class="token keyword">int</span><span class="token punctuation">,</span> power <span class="token keyword">int</span><span class="token punctuation">)</span> <span class="token keyword">int</span> <span class="token punctuation">{</span>
   <span class="token keyword">var</span> result <span class="token keyword">int</span> <span class="token operator">=</span> <span class="token number">1</span>
   <span class="token keyword">if</span> power <span class="token operator">!=</span> <span class="token number">0</span> <span class="token punctuation">{</span>
   
      <span class="token comment">// Recursive function call to itself</span>
      result <span class="token operator">=</span> <span class="token punctuation">(</span>num <span class="token operator">*</span> <span class="token function">POWER</span><span class="token punctuation">(</span>num<span class="token punctuation">,</span> power<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> result
<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 class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Golang Program to calculate the power using recursion"</span><span class="token punctuation">)</span>
   <span class="token comment">// declare and initialize the integer variables</span>
   <span class="token keyword">var</span> base <span class="token keyword">int</span> <span class="token operator">=</span> <span class="token number">4</span>
   <span class="token keyword">var</span> power <span class="token keyword">int</span> <span class="token operator">=</span> <span class="token number">2</span>
   <span class="token keyword">var</span> result <span class="token keyword">int</span>
   
   <span class="token comment">// calling the POWER() function</span>
   result <span class="token operator">=</span> <span class="token function">POWER</span><span class="token punctuation">(</span>base<span class="token punctuation">,</span> power<span class="token punctuation">)</span>
   
   <span class="token comment">// Print the result using in-built function fmt.Printf()</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"%d to the power of %d is: %d\n"</span><span class="token punctuation">,</span> base<span class="token punctuation">,</span> power<span class="token punctuation">,</span> result<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

Golang Program to calculate the power using recursion
4 to the power of 2 is: 16

Description of the Code

  • In the above program, we first declare the package main.

  • We imported the fmt package that includes the files of package fmt

  • Next we create a function POWER() to calculate the power using direct recursion technique

  • We will use an if conditional statement which allows you to execute one block of code if the specified condition is true and then recursive call to the function itself

  • Now start the function main().GO program execution starts with the function main(). Declare the integer variables base, power and result

  • Now calling the POWER() function

  • And finally printing the result on the screen using in-built function fmt.Printf().This function is defined under the fmt package and it helps to write standard output.

Example 2: Golang Program code to calculate the Power using Indirect Recursion Method

Syntax

func recursion_1() {
   recursion_2()}
func recursion_2(){
   recursion_1()}
func main() {
   recursion_1();
}

Algorithm

  • Step 1 ? Import the package fmt.

  • Step 2 ? Create the function POWER_1().

  • Step 3 ? We will use an if?conditional statement.

  • Step 4 ? Recursive call to the function POWER_2().

  • Step 5 ? Create the function POWER_2().

  • Step 6 ? Recursive call to the function POWER_1() indirectly.

  • Step 7 ? Start the function main().

  • Step 8 ? Declare and initialize the variables.

  • Step 9 ? Call the function POWER_2 ().

  • Step 10 ? Print the result on the screen using fmt.Printf().

Example

<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 comment">// GOLANG PROGRAM TO CALCULATE THE POWER USING RECURSION</span>
<span class="token comment">// Indirect Recursion example</span>
<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 string">"fmt"</span>

<span class="token comment">// create a first Recursive function</span>
func <span class="token function">POWER_1</span><span class="token punctuation">(</span>num <span class="token keyword">int</span><span class="token punctuation">,</span> power <span class="token keyword">int</span><span class="token punctuation">)</span> <span class="token keyword">int</span> <span class="token punctuation">{</span>
   <span class="token keyword">var</span> result <span class="token keyword">int</span> <span class="token operator">=</span> <span class="token number">1</span>
   <span class="token keyword">if</span> power <span class="token operator">!=</span> <span class="token number">0</span> <span class="token punctuation">{</span>
   
      <span class="token comment">// Recursive function call to the second function</span>
      result <span class="token operator">=</span> <span class="token punctuation">(</span>num <span class="token operator">*</span> <span class="token function">POWER_2</span><span class="token punctuation">(</span>num<span class="token punctuation">,</span> power<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> result
<span class="token punctuation">}</span>
<span class="token comment">// create a second Recursive function</span>
func <span class="token function">POWER_2</span><span class="token punctuation">(</span>num <span class="token keyword">int</span><span class="token punctuation">,</span> power <span class="token keyword">int</span><span class="token punctuation">)</span> <span class="token keyword">int</span> <span class="token punctuation">{</span>
   <span class="token keyword">var</span> result <span class="token keyword">int</span> <span class="token operator">=</span> <span class="token number">1</span>
   <span class="token keyword">if</span> power <span class="token operator">!=</span> <span class="token number">0</span> <span class="token punctuation">{</span>
   
      <span class="token comment">// Recursive function call to the first function</span>
      <span class="token comment">// which calls this first function indirectly</span>
      result <span class="token operator">=</span> <span class="token punctuation">(</span>num <span class="token operator">*</span> <span class="token function">POWER_1</span><span class="token punctuation">(</span>num<span class="token punctuation">,</span> power<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> result
<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 class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Golang Program to calculate the power using recursion"</span><span class="token punctuation">)</span>
   <span class="token comment">// declare and initialize the integer variables</span>
   <span class="token keyword">var</span> base <span class="token keyword">int</span> <span class="token operator">=</span> <span class="token number">5</span>
   <span class="token keyword">var</span> power <span class="token keyword">int</span> <span class="token operator">=</span> <span class="token number">2</span>
   <span class="token keyword">var</span> result <span class="token keyword">int</span>
   
   <span class="token comment">// calling the POWER_2() function</span>
   result <span class="token operator">=</span> <span class="token function">POWER_2</span><span class="token punctuation">(</span>base<span class="token punctuation">,</span> power<span class="token punctuation">)</span>
   
   <span class="token comment">// Print the result using in-built function fmt.Printf()</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"%d to the power of %d is: %d\n"</span><span class="token punctuation">,</span> base<span class="token punctuation">,</span> power<span class="token punctuation">,</span> result<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

Golang Program to calculate the power using recursion
5 to the power of 2 is: 25

Description of the Code

  • In the above program, we first declare the package main.

  • We imported the fmt package that includes the files of package fmt.

  • Next we create a function POWER_1() to calculate the power using indirect recursion technique.

  • We will use an if conditional statement which allows you to execute one block of code if the specified condition is true and then recursive call to the second function POWER_2().

  • Next we create a function POWER_2 (). Here recursive function call to the first function is made which calls the first function POWER_1() indirectly.

  • Now start the function main().GO program execution starts with the function main().

  • Declare the integer variables base, power and result.

  • Now calling the POWER_2() function.

  • And finally printing the result on the screen using in-built function fmt.Printf().This function is defined under the fmt package and it helps to write standard output.

Conclusion

In the above two examples we have successfully complied and executed Golang Program code to calculate the power using the recursion technique. In the first example we have shown direct recursion method and in the second example we have shown indirect recursion method.

Updated on: 2022-11-15T12:09:24+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements