C# - Alternative Fizz Buzz

14. July 2011 18:00

 

I have always been a fan of the fizz buzz test. Here is another alternative to the test which does much the same but is not quite so aggressive so it is harder to make so many mistakes. The challange actually comes from project eular.

 

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. ind the sum of all the multiples of 3 or 5 below 1000.

 

The solution of course is simple.

 

 

static void Main(string[] args)
{
    DateTime Start = DateTime.Now;
    int Total = 0;
    for (int i = 1; i < 1000; i++)
    {
        if (i % 3 == 0 || i % 5 == 0)
            Total += i;
    }

    Console.WriteLine("Answer: {0}", Total);

    Console.WriteLine("Time Taken: {0}", DateTime.Now - Start);
    Console.ReadLine();
}

 

E-mail Kick it! DZone it! del.icio.us Permalink


Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading