fibonacci series in vb-VBForums
Results 1 to 3 of 3

Thread: fibonacci series in vb

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    fibonacci series in vb

    I am writting a recursive program in Visaul basic that will work out the fibonacci series. I have written some code, but it does not work. Can anyone tell me what is wrong with it.

    Private Sub CmdFindNumber_Click()
    Dim number As Integer
    Dim answer As Integer

    number = InputBox("Please enter a number to find", "Find Fib Number")
    LblNumber.Caption = number
    answer = FibNumber(number)
    LblAnswer.Caption = answer

    End Sub

    Private Function FibNumber(number As Integer) As Integer

    If (number > 2) Then
    (FibNumber(number - 2) + FibNumber(number - 1))'should call itself
    End If

    End Function

    many thanks in advance
    steve

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331
    Just work it out. this is the code that works.

    Private Sub CmdFindNumber_Click()
    Dim number As Integer
    Dim answer As Integer

    number = InputBox("Please enter a number to find", "Find Fib Number")
    LblNumber.Caption = number
    answer = FibNumber(number)
    LblAnswer.Caption = answer

    End Sub

    Private Function FibNumber(number As Integer) As Integer

    If (number > 2) Then
    FibNumber = (FibNumber(number - 2) + FibNumber(number - 1))
    Else
    FibNumber = 1
    End If

    End Function
    steve

  3. #3
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,152
    There is another thread with some valid recursive Fibonacci prgrams. A search of the maths forum should find it easily.
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.