Go Program to Generate Fibonacci Series

Introduction

The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, typically starting with 0 and 1. The sequence goes like 0, 1, 1, 2, 3, 5, 8, and so on. This guide will show you how to write a Go program to generate the Fibonacci series up to a specified number of terms.

Problem Statement

Create a Go program that:

  • Prompts the user to enter the number of terms.
  • Generates and displays the Fibonacci series up to the specified number of terms.

Example:

  • Input: 5
  • Output: 0 1 1 2 3

Solution Steps

  1. Import the fmt Package: Use import "fmt" to include the fmt package for formatted I/O operations.
  2. Write the Main Function: Define the main function, which is the entry point of every Go program.
  3. Declare Variables: Declare variables to store the first two Fibonacci numbers and the number of terms.
  4. Input the Number of Terms: Use fmt.Scanln to take input from the user for the number of terms.
  5. Generate the Fibonacci Series: Use a loop to generate the series up to the specified number of terms.
  6. Display the Fibonacci Series: Use fmt.Println to display the series.

Go Program

package main

import "fmt"

/**
 * Go Program to Generate Fibonacci Series
 * Author: https://www.javaguides.net/
 */
func main() {
    // Step 1: Declare variables to hold the number of terms and Fibonacci numbers
    var n int
    var first, second, next int = 0, 1, 0

    // Step 2: Prompt the user to enter the number of terms
    fmt.Print("Enter the number of terms: ")
    fmt.Scanln(&n)

    // Step 3: Generate and display the Fibonacci series
    fmt.Print("Fibonacci Series: ")

    for i := 0; i < n; i++ {
        if i <= 1 {
            next = i
        } else {
            next = first + second
            first = second
            second = next
        }
        fmt.Print(next, " ")
    }
    fmt.Println()
}

Explanation

Step 1: Declare Variables

  • The variable n is declared to store the number of terms. The variables first, second, and next are initialized to store the first two Fibonacci numbers and the next number in the sequence.

Step 2: Input the Number of Terms

  • The program prompts the user to enter the number of terms using fmt.Print. The fmt.Scanln function reads the input and stores it in the n variable.

Step 3: Generate the Fibonacci Series

  • The program uses a for loop to generate the Fibonacci series. For the first two terms, the values are directly assigned. For subsequent terms, the next number is calculated as the sum of the previous two (first and second). After each calculation, the variables first and second are updated to the next values in the series.

Step 4: Display the Fibonacci Series

  • The program prints each number in the Fibonacci series using fmt.Print. After the loop finishes, a newline is printed to conclude the output.

Output Example

Example 1:

Enter the number of terms: 5
Fibonacci Series: 0 1 1 2 3 

Example 2:

Enter the number of terms: 10
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 

Conclusion

This Go program demonstrates how to generate the Fibonacci series up to a specified number of terms. It covers basic programming concepts such as loops, conditional statements, and arithmetic operations. This example is useful for beginners learning Go programming and understanding the Fibonacci sequence.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top