Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8321015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:52:19+00:00 2026-06-08T22:52:19+00:00

Reading the coding horror, I just came across the FizzBuzz another time. The original

  • 0

Reading the coding horror, I just came across the FizzBuzz another time.

The original post is here: Coding Horror: Why Can’t Programmers.. Program?

For those who do not know:
FizzBuzz is a quite popular children’s game. Counting from 1 to 100,
and every time a number is divisible by 3 the string “Fizz” is called, every time
a number is divisible by 5 the string “Buzz” is called and every time a number
is divisible by 3 and 5 both strings together “FizzBuzz” are called instead of the number.

This time, I wrote the code and it took me a minute,
but there are several things that I do not like.

Here is my code:

public void DoFizzBuzz()
{
    var combinations = new Tuple<int, string>[] 
    { 
        new Tuple<int, string> (3, "Fizz"), 
        new Tuple<int, string> (5, "Buzz"), 
    };

    for (int i = 1; i <= 100; ++i)
    {
        bool found = false;

        foreach (var comb in combinations)
        {
            if (i % comb.Item1 == 0)
            {
                found = true;
                Console.Write(comb.Item2);
            }
        }

        if (!found)
        {
            Console.Write(i);
        }

        Console.Write(Environment.NewLine);
    }
}

So my questions are:

  1. How do I get rid of the bool found?
  2. Is there a better way of testing
    than the foreach?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:52:21+00:00Added an answer on June 8, 2026 at 10:52 pm

    I think what you’re trying to accomplish is a generic solution to FizzBuzz, that will work for any number of number-word combinations.

    You have a good start – I think I can answer your questions with this example:

    public void DoFizzBuzz()
    {
        var combinations = new List<Tuple<int, string>>
        { 
            new Tuple<int, string> (3, "Fizz"), 
            new Tuple<int, string> (5, "Buzz"), 
        };
    
        Func<int, int, bool> isMatch = (i, comb) => i % comb == 0;
        for (int i = 1; i <= 100; i++)
        {
            Console.Write(i);
    
            var matchingCombs = combinations.Where(c => isMatch(i, c.Item1)).ToList();
            if (matchingCombs.Any())
            {
                Console.Write(string.Join("", matchingCombs.Select(c => c.Item2)));
            }
            else
            {
                Console.Write(i);
            }
            Console.Write(Environment.NewLine);
        }
    }
    

    In practice, you would pass combinations in to the method, but I included it inside just to be concise.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was reading this article on Coding Horror: http://www.codinghorror.com/blog/2008/04/setting-up-subversion-on-windows.html I went to the downloads
I was reading some posts on Coding Horror blog about working with the horrors
I was reading about PDO and I came across the parse_ini_file function. A number
I've started reading NerdDinner tutorial from scratch. While reading and coding application I came
I was reading the Qt coding conventions docs and came upon the following paragraph:
I am reading Huffman Coding Algorithm to encode a string. I can see that
I just recently finished reading Secure Coding in C and C++ by Brian Seacord,
I'm new to Delphi coding and struggeling in reading .txt files. I am trying
$fp = fopen(http://feeds.reuters.com/Reuters/PoliticsNews?format=xml,r) or die(Error reading RSS data.); The above coding working correctly in
Reading across difference lineage of CPU created by intel , many questions aroused in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.