-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathfactorial.go
60 lines (45 loc) · 1.26 KB
/
factorial.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* This code is an implementation of factorial in Go using Dynamic
Programming. In DP, we follow bottom-up approach to reach to the
desired solution. Doing this prevents multiple calls to same function
input as in the case of recursion.*/
package main
import (
"fmt"
"os"
)
// This function takes a number as input and returns its factorial
func factorial(number int) int {
// Make an array to store the factorial in a bottom-up manner
array := make([]int, number+1)
// Set 0! to be 1
array[0] = 1
// Run a loop from 1 to the number
for i := 1; i <= number; i++ {
array[i] = i * array[i - 1]
}
// Return the factorial
return array[number]
}
func main() {
// Take number as input from the user
fmt.Print("Enter a number to calculate its factorial: ")
var number int
fmt.Scan(&number)
// If the number is less than 0, return
if(number < 0) {
fmt.Print("\nPlease enter a non-negative number.", "\n")
os.Exit(0)
}
// Call the factorial function and print the factorial
var result int = factorial(number)
fmt.Print("\nFactorial of ", number, " is: ", result, "\n")
}
/*
Sample I/O:
1)
Enter a number to calculate its factorial: 11
Factorial of 11 is: 39916800
2)
Enter a number to calculate its factorial: -32
Please enter a non-negative number.
*/