# 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:

5! = 5 * 4 * 3 * 2 * 1
= 120


## 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.

O(N)

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.

package main

// fmt package provides the function to print anything
import "fmt"

func factorial(number int64) int64 {

// if the number has reached 1 then we have to
// return 1 as 1 is the minimum value we have to multiply with
if number == 1 {
return 1
}

// multiplying with the current number and calling the function
// for 1 lesser number
factorialOfNumber := number * factorial(number-1)

// return the factorial of the current number
return factorialOfNumber
}
func main() {

// declaring the integer number using the var keyword
// whose factorial we have to find
var number int64

// initializing the variable whose factorial we want to find
number = 10

// calling the factorial() function and printing the factorial
fmt.Println("The factorial of", number, "is", factorial(number))
fmt.Println("(Finding the factorial in a recursive manner.)")
}


### 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.

package main

// fmt package provides the function to print anything
import "fmt"

func main() {

// declaring the integer number using the var keyword
// whose factorial we have to find
var number, iterator int64

// initializing the variable whose factorial we want to find
number = 9

// declaring the answer variable of int64 type and initializing with 1

// Running the for loop to find the factorial
for iterator = 1; iterator <= number; iterator++ {
}

// Printing the factorial of the respective number
fmt.Println("The factorial of", number, "is", answer)
fmt.Println("(Finding the factorial using for loop.)")
}


### 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: 29-Aug-2022

1K+ Views 