4 ms·
Here is a C version of fizzbuzz if anyone has an interview tomorrow: #include <stdio.h> int main(void) { int i = 0; for(i=1; i<=100; i++)
by kahlonel 9y ago
Here is a C version of fizzbuzz if anyone has an interview tomorrow:
#include <stdio.h>
int main(void)
{
int i = 0;
for(i=1; i<=100; i++)
{
unsigned int f = 0;
if(i % 3 == 0)
{
f = 1;
}
if(i % 5 == 0)
{
f |= 2;
}
if(f == 0)
{
printf("%d\n",i);
}
else if(f == 1)
{
puts("fizz");
}
else if(f == 2)
{
puts("buzz");
}
else if(f == 3)
{
puts("fizzbuzz");
}
}
return 0;
}
- strong_silent_t 9y agoEverytime this question comes up I am powerless to resist re-implementing it. http://codepad.org/KKut69jV http://codepad.org/KKut69jV #include <stdio.h> int main() { int i; for (i = 1; i < 101; i++) { int div3 = 0 == i % 3; int div5 = 0 == i % 5; if (div3 || div5) { printf("%s%s\n", div3 ? "fizz" : "", div5 ? "buzz" : ""); } else { printf("%d\n", i); } } return 0; } I will admit, it took me 4 tries to get this to compile.
- msla 9y agoThis is the shortest non-clever version I can come up with: #include <stdio.h> int main(void) { int i; for (i = 1; i <= 100; i++) { if ((i % 3) == 0) { printf("Fizz"); } if ((i % 5) == 0) { printf("Buzz"); } if ((i % 3) && (i % 5)) { printf("%d", i); } puts(""); } return 0; }
- geraldbauer 9y agoHere's the short ruby version from code golfing in 54 bytes: def fizzbuzz (1..100).map{|n|["%sBuzz"%x=["Fizz"][n%3]][n%5]||x||n} end puts fizzbuzz.join
- rskar 9y agoAnd now, in VBA: Sub Main() Dim i As Integer For i = 1 To 100 Dim f As Integer f = IIf(i Mod 3, 0, 1) Or IIf(i Mod 5, 0, 2) Dim buf As String If f = 0 Then buf = Format(i) Else buf = IIf(f And 1, "fizz", "") + IIf(f And 2, "buzz", "") End If Debug.Print buf Next i End Sub