Fizz Buzz C++

c plus plusThis is a “classic” fizz-buzz solution I created. For those of you who don’t already know what fizz-buzz is, it follows this basic context: “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””. This question has been asked by recruiters as a method for determining programming competency.  While the fizz-buzz question is not really difficult itself, it does force you to think about one of the most important concepts to programmers, loop structures.


#include <iostream>

using namespace std;

int main(){
for (int i = 0; i <= 100; i++) {
if ((i % 15) == 0) //for multiples of both 3 and 5
cout << "FizzBuzz" << endl;
else if ((i % 3) == 0) //multiples of 3
cout << "Fizz" << endl;
else if ((i % 5) == 0) //multiples of 5
cout << "Buzz" << endl;
else
cout << i << endl; //all other numbers
}
return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s