Dim i As Integer
Open "C:\output.txt" For Append As #1 ' VB6 does not have native console window support
For i=1 To 100
If i Mod 3 = 0 Then Print #1, "Fizz"
If i Mod 5 = 0 Then Print #1, "Buzz"
If i Mod 15 <> 0 Then Print #1, Cstr(i)
Next
Close #1
Also remembering that VB6 does not have an official console mode support, itâs necessary to waste two lines of code, openning and closing the text file used as Output.
My 2 cents in Perl. Not the shortest, but it is among the most compact and still readable, doesnât cheat using a module that does the work and is not exposed, and most importantly, it works CORRECTLY (version 5.10 and above due to use of âsayâ instead of âprintâ). Yes, it is amazing how many solutions here do not follow the actual specifications (or plain donât work):
while ($i++ < 100){
my $out;
$out = "Fizz" unless ($i%3);
$out .= "Buzz" unless ($i%5);
say "$out" || "$i";
}
This one is simpler, more readable, and no ternary ops:
while ($i++ < 100){
my $out;
$out = "Fizz" unless ($i%3);
$out .= "Buzz" unless ($i%5);
say "$out" || "$i";
}
# ... and oh, it works correctly according to the specs. One is not supposed to print the integer if the strings "fizz", "buzz", or "fizzbuzz" are printed (many solutions in this page disregard that point.
Of course we couldnât forget our both loved and hated VBA in all its glory
Normal Loop:
Sub FizzBuzz(n As Integer)
Dim i As Integer
For i = 1 To n
If i Mod 3 = 0 And i Mod 5 = 0 Then
Debug.Print "FizzBuzz"
ElseIf i Mod 3 = 0 Then
Debug.Print "Fizz"
ElseIf i Mod 5 = 0 Then
Debug.Print "Buzz"
Else
Debug.Print i
End If
Next i
End Sub
And some recursive:
Sub RecursiveFizzBuzz(n As Integer)
If n > 1 Then
RecursiveFizzBuzz (n - 1)
If n Mod 3 = 0 And n Mod 5 = 0 Then
Debug.Print "FizzBuzz"
ElseIf n Mod 3 = 0 Then
Debug.Print "Fizz"
ElseIf n Mod 5 = 0 Then
Debug.Print "Buzz"
Else
Debug.Print n
End If
Else
Debug.Print n
End If
End Sub
def FizzBuzz(highestNumber, numbers, words):
for i in range(1, highestNumber + 1):
out = ""
for number, word in zip(numbers, words):
if i % number == 0:
out += word
print(out if out else i)
FizzBuzz(100, [3,5], ["Fizz", "Buzz"])
Boom done. Reusable for different numbers and words
Python, could be more compact but this way it is more clear what the program does and less computation only mod two times instead of four.
for i in range(0,101):
fizz = i % 5 == 0
buzz = i % 3 == 0
if fizz and buzz:
print("FizzBuzz")
continue
if fizz:
print("Fizz")
continue
if buzz:
print("Buzz")
continue
print(i)
for num in range(1,100):
if num % 3 == 0:
print("Fizz", num)
elif num % 5 == 0:
print("Buzz", num)
elif num % 3 == 0 and num % 5 == 0:
print("FizzBuzz", num)
elif num % 1 == 0:
print("Prime", num)
Thank you for liking my code. I also wrote two different paper, scissors, rock program but was difficult at the start and abstract but I put mind to work and think and think with music. I am a computer science major in 4-year computer science degree program. I am really dedicating as much time to learn Python3.9.4, C++, C#, R, and several others. My main programming languages to learn is Python 3 for data science/ml/games, C# for Unity3D, C++ for Unreal Engine, R for statistical, mathematical, and graphical computing. Also, I heard software design documents are effective in computer programming but I need tips.
Also, wrote several python programs and couple (more than 10 i think) of C++ program and still learningâŚ