|
-
Nov 14th, 2002, 03:50 PM
#1
Thread Starter
New Member
Need help ASAP with a program on vb 6.0
The sequence of Fibonacci numbers begins with 1,1,2,3,5,8,13,21,34,..... and then continues with each sucessive number being the sum of the previous two.
I am to write a program that gets a number through a textbox and then prints the first Fibonacci numbers in a picture box.
Example user enters 7 then in pictue box should show 1,1,2,3,5,8,13. The first 7 fibonacci numbers.
Here is what I have and it doesn't work. I just need it basic.
Dim num1 As Integer
Dim num2 As Integer
Dim temp As Integer
Dim n As Integer
Dim i As Single
Private Sub txtnumber_Change()
n = txtnumber.Text
num1 = 1
num2 = 1
temp = 0
Picture1.Print num1; num2;
For i = 1 To n
temp = num1 + num2
i = i + 1
Picture1.Print temp;
num1 = num2
num2 = temp
Next i
End Sub
-
Nov 14th, 2002, 03:53 PM
#2
What do you mean it doesn't work? DO you get an error? Or do you get the wrong resultS?
-
Nov 14th, 2002, 03:54 PM
#3
Thread Starter
New Member
I get the wrong results. I have tried different ways but can't figure it out. Maybe someone else can.
-
Nov 14th, 2002, 03:59 PM
#4
Frenzied Member
First of all you don't need the i = i + 1 as the for loop will increment this for you. Try this....
VB Code:
Dim num1 As Integer
Dim num2 As Integer
Dim temp As Integer
Dim n As Integer
Dim i As Single
Private Sub txtnumber_Change()
n = txtNumber.Text
num1 = 1
num2 = 1
temp = 2
Picture1.Print num1 & ", " & num2
For i = 1 To n - 2
temp = num1 + num2
num1 = num2
num2 = temp
Picture1.Print ", " & num2
Next i
End Sub
seoptimizer2001
VB 6.0, VC++, VI, ASP, JavaScript, HTML,
Perl, XML, SQL Server 2000
If God had intended us to drink beer, He would have given us stomachs.
Please use the [code] and [vbcode] tags in your posts!
If you don't know how to use them please go HERE!

-
Nov 14th, 2002, 04:12 PM
#5
Thread Starter
New Member
Thanks that works for almost all. If the user inputs a 1 or 0 then 1, 1 appears instead of 1 for 1 and nothing for 0.
-
Nov 14th, 2002, 04:15 PM
#6
Addicted Member
Couldn't you store the values in an array like this:
VB Code:
Private Sub Text1_Change()
Dim Fibo() As Integer
n = Val(Text1.Text)
ReDim Preserve Fibo(n)
Fibo(0) = 1
Fibo(1) = 1
For i = 2 To n
Fibo(i) = Fibo(i - 2) + Fibo(i - 1)
Next
For i = 0 To n - 1
Picture1.Print Fibo(i)
Next
End Sub
The biggest problem with putting the code under the Text1_Change event is that if the number 20 is entered, it will print the first 2 numbers in the sequence then the first 20 right after. I would code the event on the click of a button personally.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|