Fibonacci Sequence in All Loops using VB6
Submitted by donbermoy on Wednesday, April 23, 2014 - 14:27.
We all know that Fibonacci Sequence are set of whole numbers that starts with one or zero that each number is the sum of the two preceding numbers. In this tutorial, we will create a program that can compute the fibonacci sequence of an inputted number in all kinds of looping in vb6.0.
Now, let's start this tutorial!
1. Let's start this tutorial by following the following steps in Microsoft Visual Basic 6.0: Open Microsoft Visual Basic 6.0, click Choose Standard EXE, and click Open.
2. Next, add three Buttons named Command1 and labeled it as "For Loop", Command2 and labeled it as "Do While", and Command3 and labeled it as "Do Until ". Insert a PictureBox named PictureBox1 that will be used for displaying the Fibonacci Sequence.You must design your interface like this:
3. For Button1 as the For Loop Button, put this code below.
In the code above, we used the For Loop Statement in which it allows to specify a counter, to tell it to count from one number to another each time through the loop, and to exit once the counter has reached its upper limit. We declare a series variable as integer to have an inputbox of a sequence number as the number limit of fibonacci. We have initialized variable a as 1 and the For Loop starts at x=1 up to the inputted number in the inputbox. Then, the formula c=a+b, a=b, b=c will trigger and will display the sequence in the picturebox with 2 spaces.
4. For Button2 as the Do While Button, put this code below.
The code above used Do While Loop in which each time the program reaches Loop it will check that if it is True, and if it is False, it will exit the loop. Thus, instead of exiting when an expression is True, it will exit if the expression is false.
5. For Button3 as the Do Until Button, put this code below.
The code above used Do Until Loop in which Each time the program reaches Loop it will evaluate this expression of x is greater that the variable series as inputbox minus1. If the expression is True, it will exit the loop as we have code for x=x+1, but otherwise it will continue looping of the Fibonacci Series.
- Private Sub Command1_Click()
- Dim a, b, c, series As Integer
- Picture1.Cls
- series = InputBox("Enter number of series")
- a = 1
- For x = 1 To series
- If x > 2 Then
- c = a + b
- a = b
- b = c
- Picture1.Print c & Space(2);
- Else
- Picture1.Print a & Space(2);
- b = a
- End If
- Next x
- End Sub
- Private Sub Command2_Click()
- Dim a, b, c, series As Integer
- Picture1.Cls
- series = InputBox("Enter number of series")
- a = 1
- Do While x < series
- If x > 1 Then
- c = a + b
- a = b
- b = c
- Picture1.Print c & Space(2);
- Else
- Picture1.Print a & Space(2);
- b = a
- End If
- x = x + 1
- Loop
- End Sub
- Private Sub Command3_Click()
- Dim a, b, c, series As Integer
- Picture1.Cls
- series = InputBox("Enter number of series")
- a = 1
- Do Until x > series - 1
- If x > 1 Then
- c = a + b
- a = b
- b = c
- Picture1.Print c & Space(2);
- Else
- Picture1.Print a & Space(2);
- b = a
- End If
- x = x + 1
- Loop
- End Sub
Output:
Best Regards, Engr. Lyndon R. Bermoy IT Instructor/System Developer/Android Developer Mobile: 09488225971 Telephone:826-9296 E-mail:[email protected] Follow and add me in my Facebook Account: https://www.facebook.com/donzzsky Like my page on Facebook at: https://www.facebook.com/BermzISwareAdd new comment
- 1645 views