Decimal To Octal Algorithm

The Decimal to Octal Algorithm is a technique utilized to convert a number from the decimal (base 10) numeral system to the octal (base 8) numeral system. This conversion is particularly useful in computer science and digital electronics, where the octal system offers a more manageable representation of binary numbers. The algorithm involves a series of divisions and remainders to systematically transform a decimal number into its equivalent octal representation. To perform the Decimal to Octal Algorithm, one must first divide the given decimal number by 8 and obtain the quotient and the remainder. The remainder becomes the least significant digit (rightmost) in the octal number. The quotient obtained from the initial division is then further divided by 8, and the remainder of this division becomes the next least significant digit in the octal number. This process is repeated until the quotient becomes zero. The final set of remainders, when read in reverse order, represent the desired octal number.
function decimalToOctal (num) {
  var oct = 0; var c = 0
  while (num > 0) {
    var r = num % 8
    oct = oct + (r * Math.pow(10, c++))
    num = Math.floor(num / 8) // basically /= 8 without remainder if any
  }
  console.log('The decimal in octal is ' + oct)
}

decimalToOctal(2)
decimalToOctal(8)
decimalToOctal(65)
decimalToOctal(216)
decimalToOctal(512)

LANGUAGE:

DARK MODE: