Search code examples
c#fizzbuzz

Print numbers from 1 to 100 and replace some with strings (fizzbuzz)


I am trying the fizzbuzz program from here: Why Can't Programmers.. Program?

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

protected void btn1_Click(object sender, EventArgs e)
    {
        for (int i = 1; i < 101; i++)
        {
            if (i % 3 == 0 & i % 5 == 0)
            {
                Response.Write("fizzbuzz" + ",");
            }
            else if (i % 3 == 0)
            {
                Response.Write("fizz" + ",");
            }
            else if (i % 5 == 0)
            {
                Response.Write("buzz" + ",");
            }
            else
            {
                i = i + 0;
            }

            Response.Write(i +",");
        }

    }

I am able to produce some kind of result like:

1,2,fizz,3,4,buzz,5,fizz,6,7,8,fizz,9,buzz,10,11,fizz,12,13,14,fizzbuzz,15,16,17,fizz,18,19,buzz,20,fizz,21,22,23,fizz,24,buzz,25,26,fizz,27,28,29,fizzbuzz,30,31,32,fizz,33,34,buzz,35,fizz,36,37,38,fizz,39, and so on..

The word fizz was printed but it did not replace 3 and fizzbuzz was printed but it did not replace 15 and so ...


Solution

  • Whether you hit the if condition or not you are still printing i at the end of your code.

    Look specifically at your for loop:

    for (int i = 1; i < 101; i++)         
    {             
    if (i % 3 == 0 & i % 5 == 0)             
    {                 
    Response.Write("fizzbuzz" + ",");             
    }             
    else if (i % 3 == 0)             
    {                 
    Response.Write("fizz" + ",");             
    }             
    else if (i % 5 == 0)             
    {                 
    Response.Write("buzz" + ",");             
    }             
    else             
    {                 
    i = i + 0;             
    }              
    Response.Write(i +",");   //look here you print i
    } 
    

    So you need to move that last Response.Write(i + ","); in the last else condition. The easiest way to find bugs like these is to use the debugger and debug your program. You will then easily see what the output is. So definately use the debugger and set breakpoints / watches and watch what happens. Your code should change to this:

      for (int i = 1; i < 101; i++)         
        {             
        if (i % 3 == 0 & i % 5 == 0)             
        {                 
        Response.Write("fizzbuzz" + ",");             
        }             
        else if (i % 3 == 0)             
        {                 
        Response.Write("fizz" + ",");             
        }             
        else if (i % 5 == 0)             
        {                 
        Response.Write("buzz" + ",");             
        }             
        else             
        {                 
        Response.Write(i +",");   //look here you print i
        }              
        } 
    

    Notice the removal of i=i+1 your for loop is handling this already by incrementing i.

    Edit

    Not sure if this is easier but here is another way to do this using lambda's:

                List<int> t;
                t = Enumerable.Range(1, 100).ToList();
    
                var fizzBuzz = t.Where(num => num % 3 == 0 && num % 5 == 0);
                var fizz = t.Where(num => num % 3 == 0);
                var buzz = t.Where(num => num % 5 == 0);
                var notFizzBuzz = t.Where(num => num % 3 != 0 && num % 5 !=0);
    
                //print fizzBuzz elements
                Console.WriteLine("Printing fizzBuzz elements...");
                foreach (int i in fizzBuzz)
                    Console.WriteLine(i);
    
                //print fizz elements
                Console.WriteLine("Printing fizz elements...");
                foreach (int i in fizz)
                    Console.WriteLine(i);
    
                //print buzz elements
                Console.WriteLine("Printing buzz elements...");
                foreach (int i in buzz)
                    Console.WriteLine(i);
    
                //print other elements
                Console.WriteLine("Printing all others...");
                foreach (int i in notFizzBuzz)
                    Console.WriteLine(i);