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 & 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