Home »
Code Examples »
Golang Code Examples
Golang - Demonstrate the concept of direct recursion Code Example
The code for Demonstrate the concept of direct recursion
package main
import (
"fmt"
)
func calculateFactorial(number int) int {
if number == 0 || number == 1 {
return 1
}
if number < 0 {
fmt.Println("Invalid number")
return -1
}
return number*calculateFactorial(number - 1)
}
func main() {
result1 := calculateFactorial(0)
fmt.Println(result1, "\n")
result2 := calculateFactorial(5)
fmt.Println(result2, "\n")
result3 := calculateFactorial(-1)
fmt.Println(result3, "\n")
result4 := calculateFactorial(10)
fmt.Println(result4, "\n")
}
/*
Output:
1
120
Invalid number
-1
3628800
*/
Code by IncludeHelp,
on March 1, 2023 06:59