How to find factorial of a given number in Go

Go is a modern programming language developed by Google. It was created to be efficient, concise, and easy to use. Go combines the best features of other programming languages, balancing simplicity and powerful performance.

Factorial

Factorial is a mathematical operation denoted by the exclamation mark '!', where a non-negative number is multiplied by all the positive integers below it, down to 1.

For example, the factorial of six is:

Another essential fact about factorials is that 0!=10 ! = 1

Computing factorial in Go

To compute the factorial of a given number in Go, two methods are available: the recursive approach and the iterative approach. Both ways are feasible but differ in the execution of the code and function calls.

Recursive Approach

package main
import (
"fmt"
"log"
)
// Recursive approach to calculate factorial
func factorialRecursive(n int) int {
// Base case: factorial of 0 or 1 is 1
if n == 0 || n == 1 {
return 1
}
// Recursive call to calculate factorial
return n * factorialRecursive(n-1)
}
func main() {
// Calculate factorial using recursive approach
var number int
_, err := fmt.Scanln(&number)
if err != nil {
log.Fatal("Invalid input. Please enter a valid non-negative integer.")
}
if number < 0 {
log.Fatal("Invalid input. Please enter a non-negative integer.")
}
// Calculate factorial using recursive approach
factorialRec := factorialRecursive(number)
fmt.Println("Factorial of", number, "(Recursive):", factorialRec)
}

Enter the input below

  • Lines 7–14: The recursive function factorialRecursive accepts an integer input and has a base case that returns 1 when the number is 0 or 1. The function uses a recursive approach, which means it calls itself with 1 number lower than the present until it reaches the base case. The result is then multiplied recursively, propagating the multiplication back to the root call.

  • Lines 17–24: The validation process occurs where input is obtained from the user, validated to ensure compatibility with the expected data type and that it is a non-negative integer.

  • Line 26: The recursive function is called, and the value returned is saved in the variable factorialRec.

Iterative Approach

package main
import (
"fmt"
"log"
)
// Iterative approach
func factorialIterative(n int) int {
factorial := 1
// Multiply numbers from 2 to n to calculate factorial
for i := 2; i <= n; i++ {
factorial *= i
}
return factorial
}
func main() {
// Calculate factorial using iterative approach
var number int
_, err := fmt.Scanln(&number)
if err != nil {
log.Fatal("Invalid input. Please enter a valid non-negative integer.")
}
if number < 0 {
log.Fatal("Invalid input. Please enter a non-negative integer.")
}
// Calculate factorial using iterative approach
factorialIter := factorialIterative(number)
fmt.Println("Factorial of", number, "(Iterative):", factorialIter)
}

Enter the input below

  • Lines 7–14: The iterative function factorialIterative accepts an integer input and uses a for loop to perform a sequence of multiplications from the smallest number to the input number. If the input number is 0 or 1, the function returns 1, as the factorial of 0 or 1 is defined as 1.

  • Line 26: The iterative function is called, and the value returned is saved in the variable factorialIter.

Complexity

The recursive and iterative approaches require either iterating over a range from 1 to N or making approximately N function calls in the case of the recursive process. Consequently, the time complexity for both approaches can be considered as O(N).

Regarding space complexity, neither of the methods requires storing the intermediate results in an array or any additional data structure. As a result, the space complexity for both approaches can be considered as O(1), indicating constant space usage.


Recursive

Iterative

Time Complexity

O(N)

O(N)

Space Complexity

O(1)

O(1)

Summary

There are two different approaches used to calculate the factorial of a number. While the underlying algorithm is conceptually similar to other programming languages, Go has unique syntax and constructs.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved