
FizzBuzz is a programming challenge for beginners based on a maths game children sometimes play in school. The idea is to count up from 1 to 100 but with a twist. Every time you hit a multiple of 3 you say Fizz, every time you hit a multiple 5 you say Buzz, and every time you hit a multiple of both you say FizzBuzz. The FizzBuzz challenge is to create a program that simulates the game.
What is the FizzBuzz Challenge For?
FizzBuzz at its core is a novelty program for beginners but over time its evolved into a whole different animal. Lots of programmers like to challenge themselves and others to come up with the shortest and most efficient FizzBuzz Program possible. Its also turned into one of the most famous problems that an applicant will be asked to complete during a programming job interview.
At first FizzBuzz may not sound like the best question for an interviewer to ask an applicant but you’d be surprised by the amount of people who fail at FizzBuzz. A lot of people like to say that 99.5% of people who apply to programming jobs cant do it and I think that’s probably an overestimate but it certainly is a majority.
Before moving on with the article you should try and create your own basic FizzBuzz as I’m going to walk through a simple FizzBuzz in C++. Don’t feel too bad if you can’t manage it or if your solution is a bit clunky, its a pretty common issue for people to have, especially if they’re newer to programming.
An Example of FizzBuzz in C++
Creating a FizzBuzz program in C++ is very simple and only takes a few lines of code. After #including the iostream library the first thing that should be written into your int main function is a for statement that iterates through numbers 1-100.
#include <iostream> int main() { for(int i = 1; i<101; i++){
After this create a series of if statements that check if each number is a multiple of 3, 5 or both. If a number has a remainder of zero when divided by another number it is a multiple of that number, to check the remainder of two numbers in C++ you use the % sign. At the end insert an else statement which will print out the number if it doesn’t fall into any of the above categories.
if(i%5==0&&i%3==0){ std::cout << "FizzBuzz"<< std::endl; } else if(i%3==0){ std::cout << "Fizz" << std::endl; } else if(i%5==0){ std::cout << "Buzz"<< std::endl; } else{ std::cout << i<< std::endl; }
Be sure to check for multiples of 3 and 5 first if you use this method otherwise either just Fizz or Buzz will print out depending on which other multiple you did write first.
*This is not the best or most efficient way to go about FizzBuzz in C++, I have seen people manage to squeeze it down to only a few lines of code before. However this is one of the simplest ways to go about it and how most people would do it if they weren’t specifically trying to make it as compact as possible.
Why Do So Many People Have Trouble With FizzBuzz?
FizzBuzz can be quite challenging for a lot of people because they struggle with the problem solving aspect of coding. This ability to problem solve is one of the most important thing for any aspiring coder as it will allow them to move beyond just the learned stuff and to create their own unique solutions and ideas which is what makes a great programmer at the end of the day.
Being able to create a working FizzBuzz program is a demonstration of problem solving skills even if it seems pretty simple. Assuming you’ve never been directly taught how to do FizzBuzz before, creating it shows that you can take what you know about your language and deduce what you need to do to make FizzBuzz work.
How You Can Get Better at FizzBuzzing
If problem solving is an area you’re struggling with in coding that doesn’t at all mean you’re an idiot or that you can’t be a programmer. It just means that you need to sharpen your problem solving abilities and there are many different methods for doing this.
One good way to become better at this is to take a problem and study solutions to it. Try to understand the process someone went through to solve this problem and why they made the decisions they did.
Having another programmer talk you through their process of problem solving can also be very helpful. Ask them why they made certain decisions when creating their code and how they came up with solutions to issues that they encountered.
If your problem is that your having trouble thinking of any solution at all then you should take a short break. It’ll give your brain a rest and you may find that when you come back to the problem you’ll have brand new insights about it.
Good Luck with FizzBuzzing.
Download the above source code here.
If you have any comments or questions email me at nick@crumbsofcode.com