# How to Convert Decimal to Octal in Java

How to Convert Decimal to Octal in Java There are two methods to convert Decimal to Octal:

• Using toOcatlString() method
• Using user-defined logic
Using Integer.toOctalString() method The toOctalString() is astaticmethod of the Integer wrapper class that converts decimal to an octal string. The signature of the method is given below: public static String toOctalString(int decimal) Example In the following example, we have invoked the toOctalString() method of Integer wrapper class. The method parses an integer argument which you want to convert and returns the octal representation of the decimal number.
```public class DecimalToOctalExample
{
public static void main(String args[])
{
System.out.println(Integer.toOctalString(21));
System.out.println(Integer.toOctalString(67));
}
}```
Output
```25
103```
Using user-defined logic You can also convert decimal to octal in using user-defined logic. Example In the following example, we have defined a method of name conversion. Inside the method body, we have taken two variables rem (represent remainder) of type integer, and octal (represent octal number) of type String. We have also taken an array octalchar[]of characters, and assign values from '0'to '7' into it. The method has a while loop that checks the condition and performs the iterations. Let’s see how the while loop will be executed. Remember: The second statement of the while loop performs concatenation instead of addition.If you change the sequence of the statement octal=octal+octalchars[rem]then the output will be 41 which is the wrong output. So remember the sequence while you write this statement. For first iteration decimal=12                                                                                         //passed parameter 12>0                                                                                                        //condition true rem=12%8 = 4                                                                                   //finds remainder octal=octalchars+octal = 4                                      //updated value of octal is 4 decimal=12/8 = 1                                                              //updated value of decimal is 1 For second iteration decimal=1 1>0                                                                                                           //conditiontrue rem=1%8 = 1                                                                                      //finds remainder octal=octalchars+octal = 14                                    //updated value of octal is 4 decimal=1/8 = 0                                                                                //updated value of decimal is 0 For third iteration decimal=0 0>0                                                                                                         //condition false The while loop terminates the execution, and returns the converted octal value. Similarly you can also perform iteration for the decimal value 75. Hence the third iteration will not be executed. The while loop terminates the execution. The method conversion returns the octalvalue.
```public class DecimalToOctalExample1
{
public static String conversion(int decimal)                           //called method
{
int rem;
String octal="";
char octalchars[]={'0','1','2','3','4','5','6','7'};
while(decimal>0)
{
rem=decimal%8;
octal=octalchars[rem]+octal;
decimal=decimal/8;
}
return octal;
}
public static void main(String args[])
{
System.out.println("Decimal to octal of 12 is: "+conversion(12));                                               //methodcalling
System.out.println("Decimal to octal of 75 is: "+conversion (75));
}
}```
Output
```Decimal to Octal of 12 is: 14
Decimal to octal of 75 is: 113```