if (args.Length != 1)
throw new Exception("Requires one input of integer");
int inputVal = 0;
int.TryParse(args[0], out inputVal);
int exponentOf8GreaterThanInput = 0;
int exponent = 0;
List<int> base8ExponentValueArray = new List<int>();
//create list/array of power of 8 exponents IE 1/8/64/512 until the value is greater than the input
//Pretend the input is 125
//We loop and build array until we get a number bigger than 125
//so we would end up with an array of 512,64,8,1
while (exponentOf8GreaterThanInput <= inputVal)
{
exponentOf8GreaterThanInput = (int)Math.Pow(8, exponent);
base8ExponentValueArray.Insert(0, exponentOf8GreaterThanInput);
exponent+=1;
}
var result = new List<string>(); //string list of integers
var amountRemaining = inputVal; //this value will store the starting amount and any remainders
//loop thru the array of powers of 8 starting largest first
//First time thru using 125 as input
// 125/512, non rounded result = 0
// 125/64 non rounded = 1 with remainder of 61
// 61/8 non rounded = 7 with remainder of 5
// 5/1 just equals 5
// result = 0175
foreach (int base8Val in base8ExponentValueArray)
{
//get a non rounded integer from division AKA 1.954 = 1
var val = Math.Floor((decimal)amountRemaining / (decimal)base8Val);
//add the non rounded integer to the result string array
result.Add(val.ToString());
//set the remainer AKA the next amount to divide.
amountRemaining = inputVal % base8Val;
}
//loop thru result array and print each char in order they were added
foreach (var val in result)
Console.Write(val);
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.