Welcome to my blog!

Hi! My name is Resnef

Thank you for visiting my blog. I hope you'll get something helpful here. Please subscribe also to my Youtube Channel.

Looking for something?

Subscribe to this blog!

Receive the latest posts by email. Just enter your email below if you want to subscribe!

Enter your email address:

Delivered by FeedBurner

Writing a FizzBuzz Code on VB.Net



I encountered this 'FizzBuzz' thing when I was browsing the internet and saw an 
online programming test for programmers to qualify for a programming competition sponsored by a particular company (which I forgot). I was curious about this test and so I tried it. The test comes with a timer so I have to finish it before the time but I ignored it because I am not interested for the competition. All I just wanted is to try this simple yet logical test that they want me to do.

Here is the programming problem given:

Create a simple program (using any programming tool/ language) that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". - This problem sounds challenging to me. 

After reading the problem, I already have a thought in my mind. First I tried to solve this using a 'Do Until - Loop' statement since it limits only to 100. Below is the code:


 Private Sub FizzBuzzResults()
        Dim counter As Integer = 1
        Dim multiples_three As Integer = 3
        Dim multiples_five As Integer = 5
        Do Until counter = 100
            If counter = multiples_three AndAlso counter = multiples_five Then
                txt_result.Text &= "FIZZ BUZZ" & Environment.NewLine
                multiples_three += 3
                multiples_five += 5
            ElseIf counter = multiples_three Then
                txt_result.Text &= "FIZZ" & Environment.NewLine
                multiples_three += 3
            ElseIf counter = multiples_five Then
                txt_result.Text &= "BUZZ" & Environment.NewLine
                multiples_five += 5
            Else
                txt_result.Text &= counter & Environment.NewLine
            End If
            counter += 1
        Loop

    End Sub

The second option is also correct:

Private Sub FizzBuzzResults()
        For counter As Integer = 1 To 100
            Dim multiples_three As Boolean = (counter Mod 3 = 0)
            Dim Multiples_five As Boolean = (counter Mod 5 = 0)
            If multiples_three AndAlso Multiples_five Then
                txt_result.Text &= "FIZZ BUZZ"
            ElseIf multiples_three Then
                txt_result.Text &= "FIZZ"
            ElseIf Multiples_five Then
                txt_result.Text &= "BUZZ"
            Else
                txt_result.Text &= counter
            End If
            txt_result.Text &= Environment.NewLine
        Next counter
    End Sub

Third option: I have no other answer but this could be a challenge for you to do your own style and tricks.

To test the code above, grab one textbox from the toolbox and set the 'Multiline' property to 'True' and the 'Scrollbars' property to 'Vertical' then re-size the desired height and width. Next, grab one button from the toolbox and place it on your form (name it 'btn_execute'). And finally, double click on the button you just created and call the code like this call FizzBuzzResults or simply FizzBuzzResults.

The result should look like this.


Additional Info guys.

Did you know that 'FizzBuzz' test was invented by Imran Ghory which became a popular programming test used in software developer job interviews to test whether the applicant is able to write codes.