I got this requirement recently:
Write some code that prints out the following for a contiguous range of numbers:
- the number 'fizz' for numbers that are multiples of 3
- 'buzz' for numbers that are multiples of 5
- 'fizzbuzz' for numbers that are multiples of 15
e.g. if I run the program over a range from 1-20 I should get the following output
1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz 16 17 fizz 19 buzz
This attempt was done this way because I needed to print the sequence on the console and I needed to show the corresponding tests ala TDD.
public static class Evaluate
{
public static string FizzBuzz(int start, int end)
{
return Enumerable.Range(start, (end - start) + 1)
.Select(FizzOrBuzz)
.Aggregate(String.Empty, (y, x) => String.Format("{0} {1}", y, x))
.Trim();
}
public static string FizzOrBuzz(int n)
{
if (n % 15 == 0) return "fizzbuzz";
if (n % 3 == 0) return "fizz";
if (n % 5 == 0) return "buzz";
return n.ToString();
}
}
FizzOrBuzz
private, other than that everything seems fine to me \$\endgroup\$