Fibonacci Sequence in VB.net using loop
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开发者_如何学Python 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?
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
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 ...
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
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()
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
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
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
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
精彩评论