Social Items

Go Program To Generate Fibonacci Series (Golang)

Go Program To Generate Fibonacci Series (Golang) - in this post we will learn how to create a Fibonacci series program in the Go programming language. Fibonacci is a pattern of numbers obtained from the sum of the two previous numbers in a sequence.

Go Program To Generate Fibonacci Series (Golang)


Source Code : 


package main

import "fmt"

func fibonacci(n int) int {
  if n == 0 || n == 1 {
    return n
  } else {
    return (fibonacci(n-1) + fibonacci(n-2))
  }
}

func main() {
  var n, i, j int
  j = 0

  fmt.Print("Enter the number of terms: ")
  fmt.Scanln(&n)

  fmt.Print("Fibonacci series: ")
  for i = 1; i <= n; i++ {
    fmt.Print(fibonacci(j), ", ")
    j++
  }
  fmt.Println()
}

Save the source code with the name of fibonacci.go, but adjust wrote with a file name that chills and don't forget the extension should .go

Compile & Run :

Here's how to compile source code manually:

$ go build fibonacci.go
$ ./fibonacci
You can run without having to compile it:

$ go run fibonacci.go

The Output of Program :


Picture of the result Fibonacci series
Picture of the result Fibonacci series

Go Program To Generate Fibonacci Series (Golang)

Go Program To Generate Fibonacci Series (Golang)

Go Program To Generate Fibonacci Series (Golang) - in this post we will learn how to create a Fibonacci series program in the Go programming language. Fibonacci is a pattern of numbers obtained from the sum of the two previous numbers in a sequence.

Go Program To Generate Fibonacci Series (Golang)


Source Code : 


package main

import "fmt"

func fibonacci(n int) int {
  if n == 0 || n == 1 {
    return n
  } else {
    return (fibonacci(n-1) + fibonacci(n-2))
  }
}

func main() {
  var n, i, j int
  j = 0

  fmt.Print("Enter the number of terms: ")
  fmt.Scanln(&n)

  fmt.Print("Fibonacci series: ")
  for i = 1; i <= n; i++ {
    fmt.Print(fibonacci(j), ", ")
    j++
  }
  fmt.Println()
}

Save the source code with the name of fibonacci.go, but adjust wrote with a file name that chills and don't forget the extension should .go

Compile & Run :

Here's how to compile source code manually:

$ go build fibonacci.go
$ ./fibonacci
You can run without having to compile it:

$ go run fibonacci.go

The Output of Program :


Picture of the result Fibonacci series
Picture of the result Fibonacci series

Subscribe Our Newsletter