No more Fizzbuzz. How about Hello World instead? Write a program 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”


It seems that we're shooting for one fizzbuzz question for each language. I'm sick of FizzBuzz, so I'm killing two birds with one stone. This code works in both vb6 and vba.

My goal here was to write a flexible Fizzbuzzer; one that could double as a Hello World program (of sorts). Is there anything left to say about it, or are we all FizzBuzzed out?

FizzBuzzer (Standard Module):

Option Explicit

Public Function Convert(ByVal number As Integer, Optional ByVal fizzDivisor As Integer = 3, Optional ByVal buzzDivisor As Integer = 5, Optional ByVal fizzWord As String = "Fizz", Optional ByVal buzzWord = "Buzz")

    If (number Mod fizzDivisor = 0) And (number Mod buzzDivisor = 0) Then
        Convert = fizzWord &amp; buzzWord
    ElseIf (number Mod fizzDivisor = 0) Then
        Convert = fizzWord
    ElseIf (number Mod buzzDivisor = 0) Then
        Convert = buzzWord
    Else
        Convert = number
    End If

End Function


Example Calls:

Public Sub Main()

    Debug.Print "Standard Fizz Buzz"
    Dim i As Integer
    For i = 1 To 100
        Debug.Print (FizzBuzzer.Convert(i))
    Next i

    Debug.Print "Change the Numbers"
    For i = 1 To 100
        Debug.Print (FizzBuzzer.Convert(i, 4, 6))
    Next i

    Debug.Print "Change the Words"
    For i = 1 To 100
        Debug.Print (FizzBuzzer.Convert(i, fizzWord:="Hello", buzzWord:="World"))
    Next i

End Sub
Just a slight optimization: instead of evaluating the modulo twice just declare two booleans an evaluate them upfront.

Public Function Convert(ByVal number As Integer, _
                        Optional ByVal fizzDivisor As Integer = 3, _
                        Optional ByVal buzzDivisor As Integer = 5, _
                        Optional ByVal fizzWord As String = "Fizz", _
                        Optional ByVal buzzWord = "Buzz")

    Dim fizzState As Boolean
    fizzState = (number Mod fizzDivisor = 0)

    Dim buzzState As Boolean
    buzzState = (number Mod buzzDivisor = 0)

    If fizzState And buzzState Then
        Convert = fizzWord &amp; buzzWord
    ElseIf fizzState Then
        Convert = fizzWord
    ElseIf buzzState Then
        Convert = buzzWord
    Else
        Convert = number
    End If

End Function


Also you may not want to include as many newlines in the arguments as I did but you don't want a line over 200 characters long or use a horizontal scroll-bar.