Go - Factorial Program using loop
We can find the factorial of a number n by multiplying it with its predecessors up to 1. For example, if we have to find factorial of 4, the equation will look like this:
4! = 4*3*2*1 = 24.
Example 1. Go program to find factorial of a given number,
package main
import "fmt"
func main() {
var number int
fmt.Println("Enter an integer value : ")
_, err := fmt.Scanf("%d", &number)
if err != nil {
fmt.Println(err)
}
fmt.Println("Factorial of the number:", Factorial(number))
}
func Factorial(number int) int {
fact := 1
for i := 1; i <= number; i++ {
fact *= i
}
return fact
}
Output: