Q5 write a program to print the Fibonacci series up to n terms.

To get this series we have to add first two numbers of series to get the 3rd digit and if we want to get next digit then we have to add last two digits of series means 3rd and 4th. By using this we can get n number of Fibonacci series. The code for this series is as follows:

Module Module1

Dim a As Integer = 0
Dim b As Integer = 1
Dim c As Integer
Dim n, i As Integer

Sub Main()
System.Console.Write("Enter value of n=")
n = System.Console.ReadLine()
For i = 1 To n
System.Console.Write(" " & a)
c = a + b

a = b
b = c

Next
System.Console.ReadKey()

End Sub

End Module

In this program, we have declared a variables a=0, b=1, c, n, I Now we have used the Console.Write function that shows message “Enter value of n =”; and when we enter any number then this number is read by Console.ReadLine function and assign to n variable.

Here we have used a=0 and b=1 because we want to print 0 and 1 as the 1st and 2nd element of the series. Now we have used for loop and provide a condition i=1 to n means this loop will be continue till the ith value will not be equal to the entered number. Inside the loop, we have print the variable a and give some space; after that we added the a+b and assigned to the c and after that we have assigned b to a ; and c to b. This loop will be continued till the condition will not become false. The output is as follows:

fibonacci-series