Go Function recursion fibonacci()

Introduction

Recursive function that can find fib(n):

func fibonacci(n int) int { 
 switch n { //w ww .  ja  va 2 s .  co m
 case 0: 
     return 0 
 case 1: 
     return 1 
 default: 
     return fibonacci(n-1) + fibonacci(n-2) 
 } 
} 

package main /*from   w ww .j  a  v  a 2s.  co  m*/

import "fmt" 

     func fibonacci(n int) int { 
      switch n { 
      case 0: 
          return 0 
      case 1: 
          return 1 
      default: 
          return fibonacci(n-1) + fibonacci(n-2) 
      } 
     } 
     
func main() { 
   fmt.Println(fibonacci(3))
   fmt.Println(fibonacci(10))
}
   



PreviousNext

Related