GO Program to Print Fibonacci Sequence of a Number

For a better understanding of this example, we always recommend you to learn the basic topics of Golang programming listed below:

What is the Fibonacci Sequence of a number?

The Fibonacci sequence of a number is an integer numbers sequence where series starting from 0, 1 and every number after the first two is the sum of the two preceding ones. To understand more details, let us see the below example.

The Fibonacci sequence of 4 is 0 1 1 2 3

How Fibonacci Sequence is implemented in GO Program

Here we are explaining how to write a GO program to find the Fibonacci sequence. We can display data on the screen by using the built-in function fmt.println() and read data by using fmt.scanln(). These functions are defined under the fmt package and it helps to write standard output. In order to read and display standard input and output, we need to import the fmt package before starting the main function.

The logic we applied in this GO program is to declare the variable num, Then read the number, which you want to find the Fibonacci sequence, from the user and save it into the variable num. Define a function fibonacciSeries(num) to calculate the Fibonacci series of numbers. Here the function accepts the input integer number num.

In this function, it will take two initial numbers for the series, i.e., 0 and 1. Print first two terms of series like 0 and 1 respectively. Use a true for loop and declare a third variable to store the previous two values. Then print the summation of two numbers until the summation is lesser than the given number num.

Given below are the steps which are used in the GO program to print Fibonacci Sequence

ALGORITHM 

STEP 1: Import the package fmt
STEP 2: Open the main() to start the program, GO program execution starts with the main()
STEP 3: Declare the variables num.
STEP 4: Read the input number num which you want to find Fibonacci Sequence.
STEP 5: Call the function Fibonacci(num)
STEP6: Display the Fibonacci sequence of the number on the output screen.
STEP 7: Exit

Step to implement Fibonacci(num):

STEP 1: Define a function that accepts the input integer numbers
STEP 2: Set the two initial numbers for the series, i.e., 0 and 1 into variables x and y respectively.
STEP 3: Assign third variable z=y
STEP 4: Print first two terms of series i.e., x and y
STEP 5: Use for loop and print the summation of two numbers until the summation is lesser than the given num. That means the loop will do the following steps

  • Assign z=y
  • Calculate y = x + y
  •  If y >= num then nothing to print and go to step 6. Otherwise, assign x=z and print value of y  

STEP 6: Exit
 

GO Source Code

                                          package main
import "fmt"

func fibonacciSeries(num int){
  //  var z int
  x := 0
   y := 1
   z := y
   fmt.Printf("Fibonacci Series is: %d %d", x, y)
   for true{
     z = y
     y = x + y
      if y >= num{
         fmt.Println()
         break
      }
      x = z
      fmt.Printf(" %d", y)
   }
}

                                      

OUTPUT

Enter the number
4
Fibonacci Series is: 0 1 1 2 3