Java Program For Decimal to Octal Conversion Last Updated : 14 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Given a decimal number N, convert N into an equivalent octal number, i.e., convert the number with base value 10 to base value 8. The decimal number system uses 10 digits from 0-9, and the octal number system uses 8 digits from 0-7 to represent any numeric value.Illustration: Basic input/output for decimal to octal conversion.Input : 33Output: 41Input : 10Output: 12Approach 1:Store the remainder when the number is divided by 8 in an array. Divide the number by 8 now.Repeat the above two steps until the number is not equal to 0.Print the array in reverse order now. Example of converting the decimal number 33 to an equivalent octal number. Example: Java // Java program to convert a Decimal Number to Octal Number // Importing input output classes import java.io.*; // Main class class Geeks { // Method // To convert decimal to octal static void decToOctal(int n) { // Creating an Integer array of // array to store octal number int[] octalNum = new int[100]; // counter for octal number array int i = 0; while (n != 0) { // Storing remainder in octal array octalNum[i] = n % 8; n = n / 8; i++; } // Printing octal number array in reverse order for (int j = i - 1; j >= 0; j--) System.out.print(octalNum[j]); } // Method 2 // Main driver method public static void main(String[] args) { // Custom input Integer number int n = 33; // Calling the above method to convert // Decimal to Octal number decToOctal(n); } } Output41 Complexity Analysis:Time complexity: O(log N)Auxiliary space: O(1) it is using constant space for variable and array OctalNum.Approach 2:Initialize ocatalNum with 0 and countVal with 1 and the decimal number as nCalculate the remainder when the decimal number is divided by 8Update octal number with octalNum + (remainder * countval)Increment the countval by countval*10Divide the decimal number by 8Repeat from step 2 until the decimal number is zeroExample: Java // Java Program to Convert Decimal Number to Octal Number // Importing input output classes import java.io.*; // Main class class Geeks { // Method 1 // To calculate the octal value of the given // decimal number static void decimaltooctal(int deciNum) { // Initially declaring and initializing the // octal number with zero int octalNum = 0, countval = 1; int dNo = deciNum; // Condition check while (deciNum != 0) { // Decimals remainder is calculated int remainder = deciNum % 8; // Storing the octalvalue octalNum += remainder * countval; // Storing exponential value countval = countval * 10; deciNum /= 8; } // Print and display the octal number System.out.println(octalNum); } // Method 2 // Main driver method public static void main(String[] args) { // Custom input decimal number int n = 33; // Calling the Method1 to convert above number // to Octal number system decimaltooctal(n); } } Output41 Complexity Analysis:Time complexity : O(log N)Auxiliary space : O(1) Comment More infoAdvertise with us M mayur_patil Follow Improve Article Tags : Java Java Programs Practice Tags : Java Explore BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial15+ min readJava Networking15+ min readJDBC Tutorial12 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions7 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like