Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.