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.
package Conversions;

import java.util.Scanner;

/**
 * This class converts Decimal numbers to Octal Numbers
 *
 *
 */
public class DecimalToOctal {
    /**
     * Main Method
     *
     * @param args Command line Arguments
     */

    //enter in a decimal value to get Octal output
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n, k, d, s = 0, c = 0;
        System.out.print("Decimal number: ");
        n = sc.nextInt();
        k = n;
        while (k != 0) {
            d = k % 8;
            s += d * (int) Math.pow(10, c++);
            k /= 8;
        }

        System.out.println("Octal equivalent:" + s);
        sc.close();
    }
}

LANGUAGE:

DARK MODE: