How to find the Factorial of a number in Golang?

In this tutorial, we are going to write and explain the code to find the factorial of a number in Golang. The factorial is the multiplication of the number with all the numbers less than it. In this tutorial, we will see two ways to find the factorial in Golang. One is by creating a recursive function. Second, by using the for a loop.

For example, the factorial of 5 is:

<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 number">5</span><span class="token operator">!</span> <span class="token operator">=</span> <span class="token number">5</span> <span class="token operator">*</span> <span class="token number">4</span> <span class="token operator">*</span> <span class="token number">3</span> <span class="token operator">*</span> <span class="token number">2</span> <span class="token operator">*</span> <span class="token number">1</span>
<span class="token operator">=</span> <span class="token number">120</span>
</div>

A recursive approach to finding the factorial of a number

Algorithm

  • STEP 1 ? In step 1 we are declaring the number whose factorial we want to find out. The data type is int64 so that we can store the large factorial values also.

  • STEP 2 ? Now we will take the input from the user and store it into the variable we declared above.

  • STEP 3 ? Now we will call the factorial function which will find the factorial by doing the multiplication recursively.

Time Complexity:

O(N)

Space Complexity:

O(1)

Example

In this example, we are going to create a recursive function that will return the factorial of that function in the end.

<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 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">factorial</span><span class="token punctuation">(</span>number int64<span class="token punctuation">)</span> int64 <span class="token punctuation">{</span>

   <span class="token comment">// if the number has reached 1 then we have to</span>
   <span class="token comment">// return 1 as 1 is the minimum value we have to multiply with</span>
   <span class="token keyword">if</span> number <span class="token operator">==</span> <span class="token number">1</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> <span class="token number">1</span>
   <span class="token punctuation">}</span>

   <span class="token comment">// multiplying with the current number and calling the function</span>
   <span class="token comment">// for 1 lesser number</span>
   factorialOfNumber <span class="token operator">:</span><span class="token operator">=</span> number <span class="token operator">*</span> <span class="token function">factorial</span><span class="token punctuation">(</span>number<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
   
   <span class="token comment">// return the factorial of the current number</span>
   <span class="token keyword">return</span> factorialOfNumber
<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 comment">// declaring the integer number using the var keyword</span>
   <span class="token comment">// whose factorial we have to find</span>
   <span class="token keyword">var</span> number int64
   
   <span class="token comment">// initializing the variable whose factorial we want to find</span>
   number <span class="token operator">=</span> <span class="token number">10</span>
   
   <span class="token comment">// calling the factorial() function and printing the factorial</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">"The factorial of"</span><span class="token punctuation">,</span> number<span class="token punctuation">,</span> <span class="token string">"is"</span><span class="token punctuation">,</span> <span class="token function">factorial</span><span class="token punctuation">(</span>number<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">"(Finding the factorial in a recursive manner.)"</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 factorial of 10 is 3628800
(Finding the factorial in a recursive manner.)

Logic Explanation:

Let?s see how the function calls happen for the number 6.

  • The first call is factorial(6) which is returning 6 * factorial(5).

  • Now in the last function factorial(5) is called which is returning 5 * factorial(4)

  • In the last function factorial(4) is called which is calling 4 * factorial(3)

  • In the last function factorial(3) is called which is calling 3 * factorial(2)

  • In the last function factorial(2) is called which is calling 2 * factorial(1)

  • In the last function factorial(1) is called which will return 1 as the base condition matched and now we will move to the last function calls in the last in first out manner.

  • Now factorial(2) is return 2 * 1 = 2

  • factorial(3) is returning 3 * 2 = 6

  • factorial(4) is returning 4 * 6 =24

  • factorial(5) is returning 5 * 24 = 120

  • factorial(6) is returning 6 * 120 = 720

For loop approach to finding the factorial of a number

Algorithm

  • STEP 1 ? In step 1 we are declaring the number whose factorial we want to find out. The data type is int64 so that we can store the large factorial values also.

  • STEP 2 ? Now we will take the input from the user and storing into the variable we declared above.

  • STEP 3 ? Now we will run the for loop to find the factorial

Example

In this example, we are going to use the for loop to find the factorial of a number given by the user as input.

<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 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">// declaring the integer number using the var keyword</span>
   <span class="token comment">// whose factorial we have to find</span>
   <span class="token keyword">var</span> number<span class="token punctuation">,</span> iterator int64
   
   <span class="token comment">// initializing the variable whose factorial we want to find</span>
   number <span class="token operator">=</span> <span class="token number">9</span>
   
   <span class="token comment">// declaring the answer variable of int64 type and initializing with 1</span>
   <span class="token keyword">var</span> answer int64 <span class="token operator">=</span> <span class="token number">1</span>
   
   <span class="token comment">// Running the for loop to find the factorial</span>
   <span class="token keyword">for</span> iterator <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> iterator <span class="token operator"><=</span> number<span class="token punctuation">;</span> iterator<span class="token operator">++</span> <span class="token punctuation">{</span>
      answer <span class="token operator">=</span> answer <span class="token operator">*</span> iterator
   <span class="token punctuation">}</span>
   
   <span class="token comment">// Printing the factorial of the respective number</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">"The factorial of"</span><span class="token punctuation">,</span> number<span class="token punctuation">,</span> <span class="token string">"is"</span><span class="token punctuation">,</span> answer<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">"(Finding the factorial using for loop.)"</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 factorial of 9 is 362880
(Finding the factorial using for loop.)

Logic Explanation

Let?s see how to use for loop in finding the factorial of a number that is equal to 6.

  • In the first iteration, we are multiplying the answer by 1 so answer = 1 * 1 = 1.

  • In the second iteration, we are multiplying the answer by 2 so answer = 1 * 2 = 2.

  • In the third iteration, we are multiplying the answer by 3 so answer = 2 * 3 = 6.

  • In the fourth iteration, we are multiplying the answer by 4 so answer = 6 * 4 = 24.

  • In the fifth iteration, we are multiplying the answer by 5 so answer = 24 * 5 = 120.

  • In the sixth iteration, we are multiplying the answer by 6 so answer = 120 * 6 = 720.

Conclusion

This is how we have to find the value of 6! I.e 720 using for loop. These are the two different ways to find the factorial of a number. To learn more about go you can explore these tutorials.

Updated on: 2022-08-29T06:55:21+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements