Skip to content

Commit 3e46bd9

Browse files
authored
Create fibonacci_iterative.go
1 parent 513070d commit 3e46bd9

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/go/fibonacci_iterative.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func FibonacciIterative(n int) int {
6+
if n <= 1 {
7+
return 1
8+
}
9+
10+
a, b := 1, 1
11+
for i := 2; i <= n; i++ {
12+
a, b = b, a+b
13+
}
14+
return b
15+
}
16+
17+
func main() {
18+
n := 9
19+
fmt.Println("Fibonacci Iterative:", FibonacciIterative(n))
20+
}

0 commit comments

Comments
 (0)