Java Program to Convert Octal Number to Decimal and vice-versa
What is an Octal Number?
In Java, an octal number is a number represented in the base-8 number system, which uses digits from 0 to 7. To denote an octal number in Java, the number is preceded by a 0 (zero) prefix. For example, the octal number 27 in Java would be represented as 033, where the prefix 0 indicates that the number is in octal format.
Steps to Convert Octal number into Decimal :
- Write down the octal number you want to convert.
- Starting from the rightmost digit, assign a power of 8 to each digit of the octal number, starting with 8^0, then 8^1, then 8^2, and so on, increasing the exponent by 1 for each digit.
- Multiply each digit of the octal number by its corresponding power of 8.
- Add the results of the multiplication from step 3 to get the decimal equivalent of the octal number.
Example : Convert the Octal Number 263 to Decimal.
- The octal number is 263.
- Assign a power of 8 to each digit, starting from the rightmost digit:
3 × 8^0 = 3
6 × 8^1 = 48
2 × 8^2 = 128 - Multiply each digit by its corresponding power of 8:
3 × 1 = 3
6 × 8 = 48
2 × 64 = 128 - Add the results of the multiplication:
3 + 48 + 128 = 179
Therefore, the decimal equivalent of the octal number 263 is 179.
Example 1 : Convert Octal Number into Decimal .
public class Main { public static void main(String[] args) { // define the octal number as a string String octalStr = "345"; // convert the octal number to decimal int decimalNum = 0; int power = 0; for (int i = octalStr.length() - 1; i >= 0; i--) { int digit = octalStr.charAt(i) - '0'; decimalNum += digit * Math.pow(8, power); power++; } // display the decimal equivalent of the octal number System.out.println("The decimal equivalent of octal number " + octalStr + " is " + decimalNum); } }
Output :
The decimal equivalent of octal number 345 is 229
Explanation :
In this example, the octal number is defined as a string literal in the variable octalStr. The conversion from octal to decimal is done using a for loop that iterates over each digit of the octal number in reverse order. Inside the loop, the code extracts each digit by subtracting the ASCII code for the character '0' from the corresponding character in the string. The decimal equivalent of the octal number is then calculated by multiplying each digit by the corresponding power of 8, and summing the results.
Steps to convert a decimal number to an octal number :
- Divide the decimal number by 8.
- Write down the remainder from step 1 as the least significant digit of the octal number.
- If the quotient from step 1 is greater than 0, repeat steps 1 and 2 using the quotient as the new decimal number.
- Write the digits obtained from step 2 in reverse order to obtain the octal number.
Example : Convert the decimal number 85 to an octal number.
- 85 divided by 8 is 10, with a remainder of 5.
- The least significant digit of the octal number is 5.
- 10 divided by 8 is 1, with a remainder of 2.
- The next digit of the octal number is 2.
- 1 divided by 8 is 0, with a remainder of 1.
- The most significant digit of the octal number is 1.
- Writing down the digits obtained in reverse order, we obtain the octal number 125.
So, the decimal number 85 is equivalent to the octal number 125.
Example 2 : Convert a decimal number to an octal number.
public class Main { public static void main(String[] args) { // define the decimal number int decimalNum = 85; // convert the decimal number to octal int octalNum = 0; int power = 0; while (decimalNum > 0) { int digit = decimalNum % 8; octalNum += digit * Math.pow(10, power); decimalNum /= 8; power++; } // display the octal equivalent of the decimal number System.out.println("The octal equivalent of decimal number is " + octalNum); } }
Output :
The octal equivalent of decimal number is 125
Explanation :
In this example, the decimal number is defined as an integer literal in the variable decimalNum. The conversion from decimal to octal is done using a while loop that repeatedly divides the decimal number by 8 and calculates the remainder. The remainder is used as the next digit of the octal number, which is accumulated in the variable octalNum. The loop continues until the decimal number becomes 0.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment