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?
Fibonacci series means, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 etc. The first two numbers of Fibonacci series are 0 and 1. Like. 0+1=1, 1+1=2, 1+2=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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With