Function Fibonacci Learn programming Visual Basic (VB.net)

Lesson:

Functions


Exercise:

Function Fibonacci


Objetive:

Create a Visual Basic (VB.net) program that uses recursion to calculate a number in the Fibonacci series (in which the first two items are 1, and for the other elements, each one is the sum of the preceding two).


Code:

Imports System
Public Class exercise108
    Public Shared Function Fibonacci(ByVal number As Integer) As Integer
        If (number = 1) OrElse (number = 2) Then
            Return 1
        Else
            Return Fibonacci(number - 1) + Fibonacci(number - 2)
        End If
    End Function

    Public Shared Sub Main()
        Dim number As Integer
        Console.Write("Enter a number: ")
        number = Convert.ToInt32(Console.ReadLine())
        Console.WriteLine("Fibonacci of {0} is {1}", number, Fibonacci(n))
    End Sub
End Class