forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonacci.go
79 lines (50 loc) · 1.17 KB
/
fibonacci.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
This is a simple program written in go language.
This program takes in a number as an input an
returns the Fnth term in the fibonacci sequence.
Here the base case is Fo =1 and F1 =1
Formula goes as : Fn = Fn-1 + Fn-2
*/
package main
import (
"fmt"
)
/* This function takes the given number and
returns the fibonacci number*/
func fibonacci(n int) int{
// base case as Fo =1 and F1 =1
if(n == 0 || n == 1){
return n
}else {
// Fn = Fn-1 + Fn-2
return fibonacci(n-1) + fibonacci(n-2)
}
}
//driver function
func main() {
var number int
fmt.Print("Enter the number :")
//taking the input from here
fmt.Scan(&number)
var res int
res = 0
/* calling the fibonacci functuion to return
the value and printing it out*/
res = fibonacci(number)
fmt.Print("The number is : ",res)
}
/*
Sample I/O :
a) Base Case :
Enter the number :0
The number is : 0
Enter the number :1
The number is : 1
b) At random number n :
Output-1:
Enter the number :20
The number is : 6765
Output-2:
Enter the number :40
The number is : 102334155
*/