Search code examples
c#decimaloctal

C# Decimal To Octal


There have been many questions but i can't seem to find the why in the answers. It's usually: no, replace this with this or this should work.

My task is to create a program that asks the user to input a 3 digit positive integer (decimal) that converts it to octal.

For example, on paper: To convert the number 112 to octal. (8 is the base number for octal.)

These are the steps you would take:

  • 112 / 8 = 14 remainder = 0
  • 14 / 8 = 1 remainder = 6
  • 1 / 8 = 0 remainder = 1

Remainder from bottom to up is the octal number that represents 112 in decimal. So the octal number for 112 is 160.

I found the following program on the internet but i don't understand it fully. The comments in the program are mine. Could anyone explain it to me please?

        //declaration and initialization of variables but why is there an array?
        int decimalNumber, quotient, i = 1, j;
        int[] octalNumber = new int[100];

        //input
        Console.WriteLine("Enter a Decimal Number :");
        decimalNumber = int.Parse(Console.ReadLine());

        quotient = decimalNumber;

        //as long as quotient is not equal to 0, statement will run
        while (quotient != 0)
        {
            //this is how the remainder is calculated but it is then put in an array + 1, i don't understand this.
            octalNumber[i++] = quotient % 8;
            //divide the number given by the user with the octal base number
            quotient = quotient / 8;
        }
        Console.Write("Equivalent Octal Number is ");
        //i don't understand the code below here aswell. 
        for (j = i - 1; j > 0; j--)
            Console.Write(octalNumber[j]);
        Console.Read();

Any help is truly appreciated.


Solution

  • This is already built into .NET, Convert.ToString already does this.

    In your code, just after you have decimalNumber = int.Parse(...) you can do this:

    Console.WriteLine(Convert.ToString(decimalNumber, 8));
    Console.Read();
    

    and then remove the rest of the code.

    Now, if you're not asking how to do octal conversion in .NET but actually how that code works, here's how it works:

    This loop does the heavy lifting:

    1   while (quotient != 0)
        {
            //this is how the remainder is calculated but it is then put in an array + 1, i don't understand this.
    2       octalNumber[i++] = quotient % 8;
            //divide the number given by the user with the octal base number
    3       quotient = quotient / 8;
        }
    

    I added some numbers to the lines to make it easier writing a description.

    Basically, the loop does this (lines above correspond to points below).

    1. As long as we have a number to convert (ie. we're still not done), loop.
    2. Figure out the least significant digit, this is the remainder after dividing by 8, which is handled by the remainder operator, %, store this digit into the array in the next position.
    3. Divide by 8 to get rid of that least significant digit and move all the other digits one up

    Then loop back.

    However, since we essentially found all the digits from the rightmost side towards the left, the loop at the end writes them back out in their opposite order.

    As an exercise to the reader, try to figure out how the code in the question behaves if you:

    • Input a negative number
    • Input 0

    (hint, it doesn't behave correctly but Convert.ToString does)