FizzBuzz program
In this post, we will look at my implementation of the FizzBuzz program. Here is a brief background and writeup behind the very core idea of this seemingly simple programming assignment.
Very briefly the problem statement is to "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".
As is mentioned, "this sort of question won’t identify great programmers, but it will identify the weak ones. And that’s definitely a step in the right direction."
Here is what I could get in a short period of time after a couple of iterations. The nice thing which I like about my implementation is that it does not use any division or multiplication. It uses simple addition instead and there is no check for divisibility by 15 at all.
#include <iostream> int main() { int three {3}, five {5}; std::string s, threes {"Fizz"}, fives{"Buzz"}; for(int i = 1; i <= 100; i++) { s = ""; if(i == three) { s = threes; three += 3; } if(i == five) { s += fives; five += 5; } if(!s.empty()) std::cout << s << std::endl; else std::cout << i << std::endl; } }