Saturday, December 15, 2012



here is my tutorial how to create a fibonacci series using for loop in visual basic . net please comment below if you some questions and clarifications.


'the code below is for checking if the txtSequence is blank
        ' if the sequence is blank a message box will popup
        If txtSequence.Text = "" Then
            MsgBox("Please enter a sequence")
            txtSequence.Focus()

            'this code below will check if the txtSequence text is digit
            'if the txtSequence is not a digit then a message box will pop up
        ElseIf Not Char.IsDigit(txtSequence.Text) Then
            MsgBox("Please enter a number")
            txtSequence.Clear()
            txtSequence.Focus()

        Else
            Dim sequence As Integer = txtSequence.Text
            Dim firstnum As Double = 0
            Dim secondnum As Double = 1
            Dim thirdnum As Double

            'the code below will output the firstnum and the second number
            txtAnswer.Text = firstnum & ", " & secondnum & ", "

            'this code is called for loop it will loop until sequence is equal to
            'after every loop the value of sequence will decrease by 1
            For ctr = 0 To sequence - 1
                'the code below will add the firstnumber and the second number
                'the sum of both number will be placed at the variable thirdnum
                thirdnum = firstnum + secondnum
                txtAnswer.Text += thirdnum & ", "
                'now the value of the second number will be placed at the variable firstnum
                firstnum = secondnum
                'now the value of the third number will be placed at  the variable secondnum
                secondnum = thirdnum
                'after executing the commands it will go back at the for loop to check if the value of sequence is zero
                'if the value of sequence is not zero it will continue the for loop and the value of sequence will decrease by 1
            Next

        End If
Categories:

1 comment:

  1. Code works as expected with lucid explanations

    however the variable "ctr" was declared but used
    can you explain that

    ReplyDelete

Subscribe to RSS Feed Follow me on Twitter!