In this program, you’ll learn to display fibonacci series in Go using for and while loops. You’ll learn to display the series upto a specific term or a number.
1 2 3 | 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …….. |
The Fibonacci series is a series where the next term is the sum of pervious two terms. The first two terms of the Fibonacci sequence is 0 followed by 1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package main import "fmt" func main() { n := 10 t1 := 0 t2 := 1 //fmt.Printf("First %d terms: ", n) fmt.Print("First ", n, " terms: ") for i := 0; i < 10; i++ { fmt.Print(t1, " , ") sum := t1 + t2 t1 = t2 t2 = sum } } |
Output:
1 2 3 | First 10 terms: 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , |