DEV Community

Cover image for FizzBuzz? More than one line doesn't count!
Adam K Dean
Adam K Dean

Posted on

2

FizzBuzz? More than one line doesn't count!

Just doing research over at r/cscareerquestions when I stumbled upon a thread about FizzBuzz.

I've heard of it, but I've never actually done it. I decided to do it and put myself to the test.

Basically the rule is:

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".

Seems simple enough:

static void EasyFizzBuzz()
{
    for (int i = 1; i <= 100; i++)
    {
        if (i % 3 == 0 && i % 5 == 0) Debug.WriteLine("FizzBuzz");
        else if (i % 3 == 0) Debug.WriteLine("Fizz");
        else if (i % 5 == 0) Debug.WriteLine("Buzz");
        else Debug.WriteLine(i);
    }
}
Enter fullscreen mode Exit fullscreen mode

But that's too easy. I'm sure everyone comes up with that solution. I'd rather stand out if I ever get asked, and nothing stands out more than a giant line of ternary operators. (Does it count as one line if I've put it onto the next line for appearance sake? Only has one semi-colon!)

static void OneLineFizzBuzz()
{
    for (int i = 1; i <= 100; i++) Debug.WriteLine((i % 3 == 0 && i % 5 == 0) ?
        "FizzBuzz" : (i % 3 == 0) ? "Fizz" : (i % 5 == 0) ? "Buzz" : i.ToString());
}
Enter fullscreen mode Exit fullscreen mode

Maybe I can come up with something more inventitive yet, just how creative can you be with such a simple problem?

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay