tutlogger

Writing a FizzBuzz Code on VB.Net




I encountered this Fizz Buzz 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). This test is commonly given during an interview to test whether the applicant is able to write code.

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 "Fizz Buzz".

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 created and call the code like this call FizzBuzzResults or simply FizzBuzzResults.

The result should look like this.



Thanks to Imran Ghory who invented this test. :)

SHARE THIS POST

3 comments:

  1. I have read your excellent post. This is a great job. I have enjoyed reading your post first time. I want to say thanks for this post. Thank you... order essay online cheap

    ReplyDelete
  2. BSO88 Situs Terpercaya di seluruh Indonesia

    Deposit Via Pulsa Tanpa Potongan
    ✅ Minimal depo 5k & wede 20k
    ✅ Bonus 10% member baru
    ✅ Bonus deposit harian 5%
    ✅ Bonus 10% Cashback slot
    ✅ Bonus rollingan 0,8%
    ✅ Bonus rollingan slot 0,1% ( setiap hari dibagikan )
    Link Daftar

    ReplyDelete

PRIVACY POLICY | TERMS OF SERVICE | COPYRIGHT POLICY