Skip to content

Commit 4442c03

Browse files
authored
Create factorial.go
In this Go code, we define a recursive function called factorial that takes an integer n as input and returns the factorial of that number. The main function reads a number from the user and then calls the factorial function to calculate the factorial and prints the result.
1 parent c848e11 commit 4442c03

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

go/factorial.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func factorial(n int) int {
6+
if n == 0 {
7+
return 1
8+
}
9+
return n * factorial(n-1)
10+
}
11+
12+
func main() {
13+
var n int
14+
fmt.Print("Enter any number of your choice: ")
15+
fmt.Scan(&n)
16+
fact := factorial(n)
17+
fmt.Printf("The factorial of the given number is %d\n", fact)
18+
}

0 commit comments

Comments
 (0)