-
Notifications
You must be signed in to change notification settings - Fork 0
/
recursivefunction.go
39 lines (22 loc) · 1.08 KB
/
recursivefunction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// This line indicates that this is the main package, which is required for a Go program.
package main
// This line imports the fmt package, which provides functions for formatting and printing text.
import "fmt"
func factorial(n int) int {
if n == 1 {
return 1
}
return n * factorial(n-1)
}
// This is the factorial() function, which takes an integer n as input and returns the factorial of that number. It uses recursion to calculate the factorial.
func main() {
var n int
fmt.Print("Enter the Number: ")
fmt.Scan(&n)
if n < 0 {
fmt.Print("\nFactorial of a negative number is not possible")
}
result := factorial(n)
fmt.Printf("\nFactorial is %d", result)
}
// This is the main() function, which is the entry point for the program. It prompts the user to enter a number, scans that number from the standard input, and checks if the number is negative. If the number is negative, it prints an error message. Otherwise, it calls the factorial() function with the entered number as input, stores the result in a variable called result, and prints the result to the console.