The octal numbers are numbers with 8 bases and use digits from 0-7. This system is a base 8 number system. The decimal numbers are the numbers with 10 as their base and use digits from 0-9 to represent the decimal number. They also require dots to represent decimal fractions.
We have to convert a number that is in the Octal Number System to the Decimal Number System. The base in an Octal Number is 8, which means that an Octal Number will have digits ranging from 0 to 7.
For Example:
In Octal: 167
In Decimal:(7 * 80) + (6 * 81) +(1 * 82)=119
The below diagram explains how to convert an octal number (123) to an equivalent decimal value:

1. Using Integer.parseInt() method
To convert any string form to decimal, we can use type.parseType() method. For example, here we need to convert from octal to decimal, and the octal form is an integer, so we can use Integer.parseInt() to convert it.
Java Program for Octal to Decimal Number Conversion Using Integer.parseInt() Method
Java
public class GFG {
public static void main(String args[])
{
String onum = "157" ;
int num = Integer.parseInt(onum, 8 );
System.out.println(
"Decimal equivalent of Octal value 157 is: "
+ num);
}
}
|
Output
Decimal equivalent of Octal value 157 is: 111
The complexity of the above method:
Time complexity : O(1)
Auxiliary space : O(1)
2. Custom Method to Convert Octal to Decimal
Algorithm
- Start and take the octal input from the user.
- Create a result variable with an initial value of 0 to store the resultant Decimal number.
- Create a loop for getting every digit in the Input.
- Multiply each digit in the number with 8n-1, where n is the digit’s position.
- Then add it to the result.
- Store the value in Step(5) to the result variable.
- Print the result variable.
Java Program for Octal to Decimal Number Conversion Using Custom Method
Java
import java.lang.Math;
public class Main {
public static void main(String[] args)
{
int a = 167 ;
int result = 0 ;
int copy_a = a;
for ( int i = 0 ; copy_a > 0 ; i++) {
int temp = copy_a % 10 ;
double p = Math.pow( 8 , i);
result += (temp * p);
copy_a = copy_a / 10 ;
}
System.out.print( "Decimal of Octal Number (" + a
+ ") : " + result);
}
}
|
Output
Decimal of Octal Number (167) : 119
The complexity of the above method:
Time complexity: O(logN) for given octal number N
Please refer to the article Program for Octal to Decimal Conversion for more information.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
04 Aug, 2023
Like Article
Save Article