Skip to content

Instantly share code, notes, and snippets.

@damonjmurray
Last active December 23, 2015 17:39
Show Gist options
  • Save damonjmurray/6670161 to your computer and use it in GitHub Desktop.
Save damonjmurray/6670161 to your computer and use it in GitHub Desktop.
A standard FizzBuzz solution
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
namespace FizzBuzzKata
{
public class Program
{
public static void Main(string[] args)
{
const int fizzFactor = 3;
const int buzzFactor = 5;
var result = Enumerable.Range(1, 100)
.Select(i => FizzBuzz(i, fizzFactor, buzzFactor));
PrintResults(Console.Out, result);
Console.ReadLine();
}
private static string FizzBuzz(int value, int fizzFactor, int buzzFactor)
{
var result = new StringBuilder();
if (value % fizzFactor == 0) result.Append("Fizz");
if (value % buzzFactor == 0) result.Append("Buzz");
return result.ToString() == string.Empty
? value.ToString(CultureInfo.InvariantCulture)
: result.ToString();
}
private static void PrintResults(System.IO.TextWriter writer, IEnumerable<string> resultSet)
{
foreach (var result in resultSet)
writer.WriteLine(result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment