Skip to content

Instantly share code, notes, and snippets.

@JFFail
Created November 3, 2015 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JFFail/07aeb79f9850b5792cf4 to your computer and use it in GitHub Desktop.
Save JFFail/07aeb79f9850b5792cf4 to your computer and use it in GitHub Desktop.
Solution To Reddit Daily Programmer #239 - A Game Of Threes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GameOfThrees
{
class Program
{
static void Main(string[] args)
{
string strNumber;
int intNumber;
//Get the number.
Console.Write("Enter a number: ");
strNumber = Console.ReadLine();
//Try to parse that to an integer.
try
{
intNumber = Int32.Parse(strNumber);
//Now loop through.
while (intNumber != 1)
{
//Figure out what to do based on the number.
if (intNumber % 3 == 0)
{
Console.WriteLine("{0} 0", intNumber);
intNumber = intNumber / 3;
}
else if ((intNumber - 1) % 3 == 0)
{
Console.WriteLine("{0} -1", intNumber);
intNumber = intNumber - 1;
}
else if ((intNumber + 1) % 3 == 0)
{
Console.WriteLine("{0} +1", intNumber);
intNumber = intNumber + 1;
}
}
Console.WriteLine("{0}", intNumber);
}
catch
{
Console.WriteLine("Error! '{0}' isn't a number!", strNumber);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment