Created
November 3, 2015 18:23
-
-
Save JFFail/07aeb79f9850b5792cf4 to your computer and use it in GitHub Desktop.
Solution To Reddit Daily Programmer #239 - A Game Of Threes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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