Octal to Decimal Conversion in Java

Octal to Decimal Conversion

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

For example, the octal number 17 is equal to decimal 15 because:

(1 × 8¹) + (7 × 8⁰) = 8 + 7 = 15

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

Method 1: Using Loop

We extract each digit of the octal number, multiply it by the corresponding power of 8, and sum the results.

import java.util.Scanner;

public class Main {
    public static int octalToDecimal(long octal) {
        int decimal = 0, i = 0;
        while (octal != 0) {
            int lastDigit = (int) (octal % 10);
            decimal += lastDigit * Math.pow(8, i);
            octal /= 10;
            i++;
        }
        return decimal;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter an octal number: ");
        long octal = scanner.nextLong();
        
        System.out.println("Decimal equivalent: " + octalToDecimal(octal));
        scanner.close();
    }
}
            

Output:

Enter an octal number: 17
Decimal equivalent: 15

Method 2: Using Built-in Function

We can use Integer.parseInt() to directly convert an octal number to a decimal number.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter an octal number: ");
        String octal = scanner.next();
        
        int decimal = Integer.parseInt(octal, 8);
        System.out.println("Decimal equivalent: " + decimal);
        scanner.close();
    }
}
            

Output:

Enter an octal number: 17
Decimal equivalent: 15

Method 3: Using Recursion

We recursively extract each digit and multiply it by the corresponding power of 8.

import java.util.Scanner;

public class Main {
    public static int octalToDecimalRecursive(long octal, int power) {
        if (octal == 0)
            return 0;
        return (int) ((octal % 10) * Math.pow(8, power) + octalToDecimalRecursive(octal / 10, power + 1));
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter an octal number: ");
        long octal = scanner.nextLong();
        
        System.out.println("Decimal equivalent: " + octalToDecimalRecursive(octal, 0));
        scanner.close();
    }
}
            

Output:

Enter an octal number: 17
Decimal equivalent: 15
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