Decimal to Octal Conversion in Java

Decimal to Octal Conversion

Decimal to Octal conversion is the process of converting a decimal number (base-10) into its equivalent octal number (base-8). Each octal digit represents a power of 8.

For example, the decimal number 31 is equal to octal 37 because:

(3 × 8¹) + (7 × 8⁰) = 24 + 7 = 31

We will explore three methods to convert a decimal number to an octal number using Java programming.

Method 1: Using Division by 8

We divide the decimal number by 8 repeatedly, storing the remainders. These remainders represent the digits of the octal number from least significant to most significant.

import java.util.Scanner;

public class Main {
    public static void decimalToOctal(int decimal) {
        int[] octal = new int[50];
        int i = 0;

        while (decimal > 0) {
            octal[i] = decimal % 8;
            decimal = decimal / 8;
            i++;
        }

        System.out.print("Octal equivalent: ");
        for (int j = i - 1; j >= 0; j--) {
            System.out.print(octal[j]);
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a decimal number: ");
        int decimal = scanner.nextInt();
        decimalToOctal(decimal);
        scanner.close();
    }
}
            

Output:

Enter a decimal number: 31
Octal equivalent: 37

Method 2: Using Integer.toOctalString()

Java provides a built-in method Integer.toOctalString() to directly convert a decimal number to its octal string representation.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a decimal number: ");
        int decimal = scanner.nextInt();
        
        String octal = Integer.toOctalString(decimal);
        System.out.println("Octal equivalent: " + octal);
        scanner.close();
    }
}
            

Output:

Enter a decimal number: 31
Octal equivalent: 37

Method 3: Using Recursion

We can convert a decimal number to octal recursively. The recursive function divides the number by 8 and prints the remainders, which form the octal digits.

import java.util.Scanner;

public class Main {
    public static void decimalToOctalRecursive(int decimal) {
        if (decimal == 0) {
            return;
        }
        
        decimalToOctalRecursive(decimal / 8);
        System.out.print(decimal % 8);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a decimal number: ");
        int decimal = scanner.nextInt();
        
        if (decimal == 0) {
            System.out.println("Octal equivalent: 0");
        } else {
            System.out.print("Octal equivalent: ");
            decimalToOctalRecursive(decimal);
            System.out.println();
        }
        scanner.close();
    }
}
            

Output:

Enter a decimal number: 31
Octal equivalent: 37
Numbers

Below You will find some of the most important codes in languages like C, C++, Java, and Python. These codes are of prime importance for college semester exams and online tests.

Getting Started

HCF - Highest Common Factor: C C++ Java Python

LCM - Lowest Common Multiple: C C++ Java Python

GCD - Greatest Common Divisor: C C++ Java Python

Binary to Decimal Conversion: C C++ Java Python

Octal to Decimal Conversion: C C++ Java Python

Hexadecimal to Decimal Conversion: C C++ Java Python

Decimal to Binary Conversion: C C++ Java Python

Decimal to Octal Conversion: C C++ Java Python

Decimal to Hexadecimal Conversion: C C++ Java Python

Binary to Octal Conversion: C C++ Java Python

Quadrants in which a given coordinate lies: C C++ Java Python

Addition of Two Fractions: C C++ Java Python

Calculate the Area of a Circle: C C++ Java Python

Convert Digit/Number to Words: C C++ Java Python

Finding Roots of a Quadratic Equation: C C++ Java Python