Skip to content

Instantly share code, notes, and snippets.

@TheBuzzSaw
Created July 31, 2017 23:44
Show Gist options
  • Select an option

  • Save TheBuzzSaw/b86a33a0ef913d12efa5fef1bd64bfba to your computer and use it in GitHub Desktop.

Select an option

Save TheBuzzSaw/b86a33a0ef913d12efa5fef1bd64bfba to your computer and use it in GitHub Desktop.
FizzBuzz with minimized redundant code
#include <iostream>
using namespace std;
struct Pair
{
int multiple;
const char* word;
};
int main(int argc, char** argv)
{
Pair pairs[] = {
{3, "Fizz"},
{5, "Buzz"},
{7, "Baz"}};
for (int i = 1; i <= 100; ++i)
{
bool outputValue = true;
for (auto pair : pairs)
{
if (!(i % pair.multiple))
{
cout << pair.word;
outputValue = false;
}
}
if (outputValue) cout << i;
cout << '\n';
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment