Here are some useful FizzBuzz programming examples in various programming languages. These might prove useful as code samples that could be expanded into a larger project or just for interests sake. Very useful if you wish to learn how to program, taking these programs apart and modifying them would be a lot of fun.
In Ruby.
for x in 0..100 do if x != 0 print "fizz" if a = ( x % 3 ).zero? print "buzz" if b = ( x % 5 ).zero? end print x unless ( a || b ) print "\n" end |
A FizzBuzz solution in C++.
#include <iostream> int main() { for(int i = 1; i =< 100; i++) { if (i % 15) std::cout << "fizzbuzz" << std::endl; else if (i % 3) std::cout << "fizz" << std::endl; else if (i % 5) std::cout << "buzz" << std:endl; else std::cout << i << std:endl; } return 0; } |
In C.
#include <stdio.h> int main(void) { int i; for(i = 0; i <= 100; i++) { __int128 x, y, z; __int128 f = 2054842726; __int128 b = 2054845794; x = f * (i % 3 == 0) + ((b * (i % 5 == 0)) << (32 * (i % 3 == 0))); y = (48 + (i % 10)) * (1 + (i >= 10) * 255) + (i >= 10) * (48 + ((i / 10) % 10)); z = x == 0 ? y : x; puts((const char *)&z); } return 0; } |
In C++.
#include <iostream> using namespace std; int main() { for (int i=0;i<=100;i++) { if ((i&3)==0) cout << "Fizz\n"; else if ((i%5)==0) cout << "Buzz\n"; else if ((i%3&&i%5)==0) cout << "FizzBuzz\n"; else cout << i << "\n"; } return 0; } |
Another version of FizzBuzz in C.
#include <stdio.h> int main(void) { int i; for(i = 0; i <= 100; i++) { __int128 x = 2054845794; x = 2054842726 * (i % 3 == 0) + ((x * (i % 5 == 0)) << (32 * (i % 3 == 0))); x += (x == 0) * ((48 + (i % 10)) * (1 + (i >= 10) * 255) + (i >= 10) * (48 + ((i / 10) % 10))); puts((const char *)&x); } return 0; } |
FizzBuzz in bash.
for i in {1..100}; do if [[ $(($i % 5)) -eq 0 ]] && [[ $(($i % 3)) -eq 0 ]]; then echo "FizzBuzz" elif [[ $(($i % 5)) -eq 0 ]]; then echo "Buzz" elif [[ $(($i % 3)) -eq 0 ]]; then echo "Fizz" else echo $i fi done |
This is a very obfuscated version of FizzBuzz for those of you that like obfuscated code.
#include <stdio.h> int main(void) { int i; for(i = 0; i <= 100; i++) { __int128 x = 2054842726; x = x * (i % 3 == 0) + (((x + 3068) * (i % 5 == 0)) << (32 * (i % 3 == 0))); x += (x == 0) * ((48 + (i % 10)) * (1 + (i >= 10) * 255) + (i >= 10) * (48 + ((i / 10) % 10))); puts((const char *)&x); } return 0; } |
FizzBuzz in Python.
import sys for i in range(0,101): if i % 3 == 0 and i % 3 == 0: print "fizzbuzz" elif i % 3 == 0: print "fizz" elif i % 5 == 0: print "buzz" else: print i |
FizzBuzz in Ruby.
def multiple_of(*factors) proc do |number| factors.all? do |factor| number.modulo(factor).zero? end end end (1..100).each do |number| puts case number when multiple_of(3, 5) then 'FizzBuzz' when multiple_of(3) then 'Fizz' when multiple_of(5) then 'Buzz' else number end end |
Here is a FizzBuzz program generator.
#include <iostream> #include <fstream> using namespace std; int main() { ofstream f; f.open("fizzbuzz.cpp"); f << "#include <iostream>" << endl; f << "using namespace std;" << endl; f << "int main()" << endl; f << "{" << endl; for (int i=0;i<=100;i++) { if ((i&3)==0) f << " cout << \"Fizz\" << endl;" << endl; else if ((i%5)==0) f << " cout << \"Buzz\" << endl;" << endl; else if ((i%3&&i%5)==0) f << " cout << \"FizzBuzz\" << endl;" << endl; else f << " cout << " << i << " << endl;" << endl; } f << " return 0;" << endl; f << "}" << endl; f.close(); return 0; } |
Here is a final code section in Java.
public class FizzBuzz { public static void main(String[] args) { for (int i = 0; i <= 100; i++) { if (i % 5 == 0 && i % 3 == 0) { System.out.println("fizzbuzz"); } else if (i % 5 == 0) { System.out.println("buzz"); } else if (i % 3 == 0) { System.out.println("fizz"); } else { System.out.println(i); } } } } |
These examples show how to solve this programming problem using simple code. There are many ways to solve this dilemma and hopefully this post will help you out.
If you run a FizzBuzz solution; this is what you should get.
[homer@deusexmachina Documents]$ ./a.out fizzbuzz 1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz 16 17 fizz 19 buzz fizz 22 23 fizz buzz 26 fizz 28 29 fizzbuzz 31 32 fizz 34 buzz fizz 37 38 fizz buzz 41 fizz 43 44 fizzbuzz 46 47 fizz 49 buzz fizz 52 53 fizz buzz 56 fizz 58 59 fizzbuzz 61 62 fizz 64 |
Have fun looking at all of these FizzBuzz examples, they will really help you understand how this solution can be coded in many simple and obfuscated ways.