6

Please could you help me with displaying the first 10 Fibonacci numbers. My code displays the following result: 1, 2, 3, 5, 8, 13, 21, 34, 55 and I need it to also display the first two Fibonacci numbers (0 and 1). How would I do that?

Public Class Form1
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim a As Integer = 0
    Dim b As Integer = 1
    Dim fib As Integer = 0

    Do
      fib = a + b
      a = b
      b = fib
      Label1.Text = Label1.Text + fib.ToString & ControlChars.NewLine
    Loop While fib < 55
  End Sub
End Class

Where in professional programming would you need to use Fibonacci sequences?

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
Wannabe
  • 493
  • 3
  • 10
  • 18

9 Answers9

3

Just add

Label1.Text = Label1.Text + a.ToString & ControlChars.NewLine
Label1.Text = Label1.Text + b.ToString & ControlChars.NewLine

before the Do ... while.

For applications linked to Fibonacci numbers see : Fibonacci: Applications

log0
  • 10,489
  • 4
  • 28
  • 62
3

Instead of calculating the next in sequence number and then adding the results to the output, do it in reverse order:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim a As Integer = 0
    Dim b As Integer = 1
    Dim fib As Integer 

    Do
        Label1.Text += a.ToString & ControlChars.NewLine
        fib = a + b
        a = b
        b = fib
    Loop While a <= 55

End Sub
Anax
  • 9,122
  • 5
  • 34
  • 68
1
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    Dim a As Integer = 0
    Dim b As Integer = 1
    Dim fib As Integer
    Dim userinput, i As Integer
    userinput = InputBox("how many")
    i = userinput
    ListView3.Items.Add(1)
    Do
        fib = a + b
        a = b
        b = fib
        ListView3.Items.Add(fib)
        i = i + 1
    Loop While fib < i
End Sub

End Class

1

In the same way that you have defined the first two fibonacci numbers in your code to be 0 and 1 you should put them into the label string at the beginning (i.e. not in the loop). You should also probably use a loop condition on the number of fibonacci numbers you've calculated instead of relying on knowing what the 10th one is.

I've never used Fibonacci numbers at work however they are quite a good learning exercise with the naive recursive soloution, one with a lookup table, a simple iterative soloution (like yours), using the golden ratio, the matrix form ...

brain
  • 5,496
  • 1
  • 26
  • 29
0

Try this code:

Dim arr As New ArrayList()
    Console.Write("The Fibonacci Series is : ")
    For i As Integer = 0 To 10
        If i = 0 Or i = 1 Then
            arr.Add(i)
            Console.Write(arr(i).ToString() + ", ")               
        Else
            arr.Add(arr(i - 2) + arr(i - 1))
            If i = 10 Then
                Console.Write(arr(i).ToString())
            Else
                Console.Write(arr(i).ToString() + ", ")
            End If
        End If
    Next
    Console.Read()
Jk1
  • 11,233
  • 9
  • 54
  • 64
0

Pretty Symple, just using a button, and you can generate as many numbers of the sequence as you want.

Sub fibonacci()

mycount = Application.CountA(Range("A:A"))

e = mycount - 1
fib = 0
fib = Cells(e, 1).Value + Cells(e + 1, 1).Value
Cells(mycount + 1, 1).Value = fib
mycount = mycount + 1

End Sub
0

Sub Main()

    Dim previousfibo As Integer = 0
    Dim currentfibo As Integer = 1
    Dim nextfibo As Integer
    previousfibo = 0
    currentfibo = 1
    Console.WriteLine(previousfibo)
    Console.WriteLine(currentfibo)
    For I = 1 To 9

        nextfibo = previousfibo + currentfibo
        Console.WriteLine(nextfibo)
        previousfibo = currentfibo
        currentfibo = nextfibo
    Next I
    Console.ReadLine()




End Sub
nadia
  • 1
-1
Dim a, b, c as integer

a=0

b=1

print a 

print b

while c<(n-c)

c=a+b

print c

a=b

b=c

wend

print "This is Fibonacci Series"

End Sub
Rushi
  • 4,553
  • 4
  • 33
  • 46
-1
Dim n As integer 
n= inputBox("ENTER A NUMBER")
Dim a As integer 
Dim b As integer 
Dim I As integer 
a=0
b=1
Print b
for I= 1To n
c= a+b
Print c
a=b
b=c
Next
End Sub
Willie Cheng
  • 7,679
  • 13
  • 55
  • 68
Elisha
  • 1