# Convert decimal to octal in java

Let’s learn convert decimal to octal in java.

## Convert decimal to octal in java

In the below example we are going to convert decimal number (base value 10) to an octal number (base value 8).

To represent numeric value, decimal number system uses 0-9 digit and octal number system uses 0-7 digit.

There are two ways to a convert decimal number to octal; one using Integer.toOctalString() method and the second using custom logic. Here’s the syntax of Integer.toOctalString() method.

Syntax:

public static String toOctalString(int i)

Parameters:

i an integer to be converted to a string.

Returns:

toOctalString() method returns the string representation of the unsigned integer value represented by the argument in octal (base 8). Now let’s see program to convert decimal to octal.

```import java.util.Scanner;
public class DecimalToOctal
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Please enter decimal number: ");
int number = sc.nextInt();
String strOctal = Integer.toOctalString(number);
System.out.println("Octal number is: " + strOctal);
sc.close();
}
}
```

Output:

Please enter decimal number: 100
Octal number is: 144

Now let’s learn to a convert decimal number to octal using custom logic.

```public class DecimalToOctalDemo
{
static String convertToOctal(int deci)
{
int remainder;
String strOctal = "";
char[] ch = {'0','1','2','3','4','5','6','7'};
while(deci > 0)
{
remainder = deci % 8;
strOctal = ch[remainder] + strOctal;
deci = deci / 8;
}
return strOctal;
}
public static void main(String[] args)
{
System.out.println("Octal of 11 is: " + convertToOctal(11));
System.out.println("Octal of 21 is: " + convertToOctal(21));
System.out.println("Octal of 31 is: " + convertToOctal(31));
}
}
```

Output:

Octal of 11 is: 13
Octal of 21 is: 25
Octal of 31 is: 37

Also read – nested classes in java