Golang Program to Find G.C.D Using Recursion

In this tutorial we will discuss how to write a Golang program to find the greatest common divisor GCD using recursion.

The greatest common divisor (GCD) of two or more numbers is the greatest common factor number that divides them, exactly. It is also called the highest common factor (HCF). For example, the greatest common factor of 15 and 10 is 5, since both the numbers can be divided by 5.

15/5 = 3
10/5 = 2

Algorithm

  • Step 1 ? Import the package fmt

  • Step 2 ? Start function main()

  • 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 ? Call the function factorialnumber ()

  • Step 7 ? Print the result on the screen using fmt.Println()

Example 1

<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 FIND GCD USING RECURSION</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">// function prototype</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">// declare the variables</span>
   <span class="token keyword">var</span> n1 <span class="token keyword">int</span>
   <span class="token keyword">var</span> n2 <span class="token keyword">int</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 find GCD using recursion"</span><span class="token punctuation">)</span>
	
   <span class="token comment">// initialize the variables</span>
   n1 <span class="token operator">=</span> <span class="token number">36</span>
   n2 <span class="token operator">=</span> <span class="token number">60</span>
	
   <span class="token comment">// print the result using in-built function fmt.Println()</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">"G.C.D OF"</span><span class="token punctuation">,</span> n1<span class="token punctuation">,</span> n2<span class="token punctuation">,</span><span class="token string">"is"</span><span class="token punctuation">,</span><span class="token function">hcf</span><span class="token punctuation">(</span>n1<span class="token punctuation">,</span>n2<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token comment">// create the function hcf()</span>
func <span class="token function">hcf</span><span class="token punctuation">(</span>n1 <span class="token keyword">int</span><span class="token punctuation">,</span> n2 <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">if</span> <span class="token punctuation">(</span>n2 <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> <span class="token function">hcf</span><span class="token punctuation">(</span>n2<span class="token punctuation">,</span> n1 <span class="token operator">%</span> n2<span class="token punctuation">)</span><span class="token punctuation">;</span>
   <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> n1<span class="token punctuation">;</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

Golang Program to find GCD using recursion
G.C.D OF 36 60 is 12

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

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

  • Next we declare and initialize the integer variables n1 and n2

  • Now create the function hcf()

  • We will use if-else conditional statements to execute the code

  • In the line of code : return hcf(n2, n1 % n2) : here the function calls itself, recursive call

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

Algorithm

  • Step 1 ? Import the package fmt

  • Step 2 ? Start function main()

  • Step 3 ? Declare and initialize the variables

  • Step 4 ? Create the function gcd()

  • Step 5 ? We will use if else conditional statement

  • Step 6 ? Recursive call to the function itself

  • Step 7 ? Print the result using fmt.Printf().

Example 2

<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 FIND GCD USING RECURSION</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>
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">// declare the variables</span>
   <span class="token keyword">var</span> a <span class="token keyword">int</span>
   <span class="token keyword">var</span> b <span class="token keyword">int</span>
   <span class="token keyword">var</span> result <span class="token keyword">int</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 find GCD using recursion"</span><span class="token punctuation">)</span>
	
   <span class="token comment">// initialize the variables</span>
   a <span class="token operator">=</span> <span class="token number">300</span>
   b <span class="token operator">=</span> <span class="token number">60</span>
   result <span class="token operator">=</span> <span class="token function">gcd</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<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">"The GCD of %d and %d is %d.\n"</span><span class="token punctuation">,</span>a<span class="token punctuation">,</span>b<span class="token punctuation">,</span>result<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token comment">// create the function gcd()</span>
func <span class="token function">gcd</span><span class="token punctuation">(</span>a <span class="token keyword">int</span><span class="token punctuation">,</span>b <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">for</span> a <span class="token operator">!=</span> b <span class="token punctuation">{</span>
      <span class="token keyword">if</span> b <span class="token operator"><</span> a <span class="token punctuation">{</span>
         <span class="token keyword">return</span> <span class="token function">gcd</span><span class="token punctuation">(</span>a <span class="token operator">-</span> b<span class="token punctuation">,</span>b<span class="token punctuation">)</span>
      <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
         <span class="token keyword">return</span> <span class="token function">gcd</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b <span class="token operator">-</span> a<span class="token punctuation">)</span>
      <span class="token punctuation">}</span>
   <span class="token punctuation">}</span>
   <span class="token keyword">return</span> a
<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 find GCD using recursion
The GCD of 300 and 60 is 60.

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

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

  • Next we declare and initialize the integer variables a, b and result

  • Variables ?a? and ?b? correspond to the integers whose GCD is to be found. Variable result corresponds to the final result after calculating the GCD.

  • Now create the function gcd()

  • We will use for loop and if-else conditional statements to execute the code. For loop is used to check that both the ?a? and ?b? variable values are not equal. If this condition is true then the loop is executed. If condition is false return the value of ?a?. If-else condition statement is used to check the value of ?a? variable is greater than the value of ?b? variable. If the condition is true, then return two integer variable values. Otherwise, if the condition is false, then execute else statement and return the values of two integer variable.

  • In the lines of code: return gcd(a - b,b): return gcd(a,b - a) : here the function calls itself, recursive call

  • 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 compiled and executed the Golang program code to find the greatest common divisor GCD using recursion technique.

We are using fmt println() and fmt.Println() functions for printing the result on the output screen. Here we are showing how to implement conditional statement in the Go language. We have used if condition in one example and for loop in second example.

Updated on: 2022-10-25T11:34:00+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements