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
- Import the fmt Package: Use import "fmt"to include the fmt package for formatted I/O operations.
- Write the Main Function: Define the mainfunction, which is the entry point of every Go program.
- Declare Variables: Declare variables to store the first two Fibonacci numbers and the number of terms.
- Input the Number of Terms: Use fmt.Scanlnto take input from the user for the number of terms.
- Generate the Fibonacci Series: Use a loop to generate the series up to the specified number of terms.
- Display the Fibonacci Series: Use fmt.Printlnto 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 nis declared to store the number of terms. The variablesfirst,second, andnextare 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. Thefmt.Scanlnfunction reads the input and stores it in thenvariable.
Step 3: Generate the Fibonacci Series
- The program uses a forloop 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 (firstandsecond). After each calculation, the variablesfirstandsecondare 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.