The decimal to octal conversion involves dividing the decimal number by 8 successively, while storing the remainders of the divisions in reverse order. The resulting sequence of remainders is the octal number equivalent.
Decimal to octal conversion is a mathematical process that involves dividing the decimal number by 8 successively until the quotient becomes zero, while storing the remainders of the divisions in reverse order. The resulting sequence of remainders is the octal number equivalent to the original decimal number.
- Divide the decimal number by 8.
- Write down the remainder.
- Divide the quotient obtained in step 1 by 8.
- Write down the remainder.
- Repeat steps 3 and 4 until the quotient becomes zero.
- The resulting sequence of remainders represents the octal number equivalent of the original decimal number.
For example, to convert the decimal number 123 to octal:
- 123 ÷ 8 = 15 with a remainder of 3. Write down 3.
- 15 ÷ 8 = 1 with a remainder of 7. Write down 7 after the 3. We now have “73”.
- 1 ÷ 8 = 0 with a remainder of 1. Write down 1 after the 7. We now have “173”.
- The resulting octal number equivalent of 123 is “173”.
Example in Javascript code:
function decimalToOctal(decimalNumber) {
let octalNumber = "";
while (decimalNumber > 0) {
let remainder = decimalNumber % 8;
octalNumber = remainder + octalNumber;
decimalNumber = Math.floor(decimalNumber / 8);
}
return octalNumber;
}
Exemple in C# code:
public static string DecimalToOctal(int decimalNumber)
{
int quotient = decimalNumber;
string octalNumber = "";
while (quotient > 0)
{
int remainder = quotient % 8;
octalNumber = remainder.ToString() + octalNumber;
quotient /= 8;
}
return octalNumber;
}