0
0
GoProgramBeginner · 2 min read

Go Program to Find Factorial of a Number

You can find the factorial of a number in Go by using a loop to multiply numbers from 1 to that number, like for i := 1; i <= n; i++ { result *= i }.
📋

Examples

Input0
OutputFactorial of 0 is 1
Input5
OutputFactorial of 5 is 120
Input10
OutputFactorial of 10 is 3628800
🧠

How to Think About It

To find the factorial of a number, multiply all whole numbers from 1 up to that number. Start with 1 because multiplying by 1 does not change the result. Keep multiplying each next number until you reach the input number.
📐

Algorithm

1
Get the input number n
2
Set result to 1
3
For each number i from 1 to n, multiply result by i
4
After the loop ends, return or print the result
💻

Code

go
package main

import "fmt"

func main() {
    var n int
    fmt.Print("Enter a number: ")
    fmt.Scan(&n)

    result := 1
    for i := 1; i <= n; i++ {
        result *= i
    }

    fmt.Printf("Factorial of %d is %d\n", n, result)
}
Output
Enter a number: 5 Factorial of 5 is 120
🔍

Dry Run

Let's trace the input 5 through the code

1

Input

User enters n = 5

2

Initialize result

result = 1

3

Loop start

i = 1, result = 1 * 1 = 1

4

Loop iteration 2

i = 2, result = 1 * 2 = 2

5

Loop iteration 3

i = 3, result = 2 * 3 = 6

6

Loop iteration 4

i = 4, result = 6 * 4 = 24

7

Loop iteration 5

i = 5, result = 24 * 5 = 120

8

Loop ends

i > n, exit loop

9

Print result

Output: Factorial of 5 is 120

iresult
11
22
36
424
5120
💡

Why This Works

Step 1: Start with 1

We set result to 1 because multiplying by 1 does not change the value and it is the identity for multiplication.

Step 2: Multiply in loop

We multiply result by each number from 1 to n to accumulate the factorial product.

Step 3: Print final result

After the loop, result holds the factorial, which we print with the input number.

🔄

Alternative Approaches

Recursive function
go
package main

import "fmt"

func factorial(n int) int {
    if n == 0 {
        return 1
    }
    return n * factorial(n-1)
}

func main() {
    var n int
    fmt.Print("Enter a number: ")
    fmt.Scan(&n)
    fmt.Printf("Factorial of %d is %d\n", n, factorial(n))
}
Uses recursion to calculate factorial; simpler code but can cause stack overflow for very large n.
Using big.Int for large numbers
go
package main

import (
    "fmt"
    "math/big"
)

func main() {
    var n int64
    fmt.Print("Enter a number: ")
    fmt.Scan(&n)

    result := big.NewInt(1)
    for i := int64(1); i <= n; i++ {
        result.Mul(result, big.NewInt(i))
    }

    fmt.Printf("Factorial of %d is %s\n", n, result.String())
}
Handles very large factorials without overflow using big integers, but code is more complex.

Complexity: O(n) time, O(1) space

Time Complexity

The loop runs from 1 to n, so the time grows linearly with n, making it O(n).

Space Complexity

Only a few variables are used regardless of input size, so space complexity is O(1).

Which Approach is Fastest?

The iterative loop is fastest and uses less memory than recursion, which adds call stack overhead.

ApproachTimeSpaceBest For
Iterative loopO(n)O(1)Small to medium numbers, fast and simple
Recursive functionO(n)O(n)Simple code, but uses more memory and risks stack overflow
big.Int for large numbersO(n)O(1) plus big.Int memoryVery large numbers without overflow
💡
Use a loop for small numbers and recursion or big.Int for larger factorials.
⚠️
Forgetting to initialize the result to 1 causes the factorial to always be zero.