# Decimal to Octal Java

Hey guys!! Welcome to flower brackets blog. Today let us learn how to convert decimal to octal java.

Below program is similar to decimal to hexadecimal conversion program. Let’s see an example on how to convert decimal to octal in java programming,

#### Example: decimal to octal java

```import java.util.Scanner;

public class DecimalToOctal
{
Scanner sc;
int number;

void getValue()
{
System.out.println("Conversion from Decimal to Octal");
sc = new Scanner(System.in);
System.out.println("Please enter a decimal number: ");
number = Integer.parseInt(sc.nextLine());
}

void toConvert()
{
String octal = Integer.toOctalString(number);
System.out.println("The converted octal value is: " + octal);
}

public static void main(String[] args)
{
DecimalToOctal dto = new DecimalToOctal();
dto.getValue();
dto.toConvert();
}
}

```

Output:

Conversion from Decimal to Octal
Please enter a decimal number: 100
The converted octal value is: 144

#### Using static method

Now let us see how to convert decimal to octal in java using a static method.

Also Read – Convert Decimal to Binary Java Example

So, let’s see an example,

```import java.util.Scanner;

public class DecimalToOctal
{
static int a = 1;
public static void main(String[] args)
{
int decimal;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a decimal number : ");
decimal = sc.nextInt();
System.out.println("The octal number is : ");
int[] oct = convertToOctal(decimal);
for(int x = a - 1; x > 0; x--)
{
System.out.print(oct[x]);
}
sc.close();
}

static int[] convertToOctal(int oct)
{
int y[] = new int[50];
while(oct != 0)
{
y[a++] = oct % 8;
oct = oct / 8;
}
return y;
}
}

```

Output:

Please enter a decimal number : 100
The octal number is : 144

#### Using recursion method

Here we are going to see java program to convert decimal to octal using recursion,

```import java.util.Scanner;

public class DecimalToOctalExample
{
static int octal[] = new int[50], x = 1;
int[] convertToOctal(int oct)
{
if(oct != 0)
{
octal[x++] = oct % 8;
oct = oct / 8;
convertToOctal(oct);
}
return octal;
}

public static void main(String[] args)
{
DecimalToOctalExample dto = new DecimalToOctalExample();
int decimal;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a decimal number: ");
decimal = sc.nextInt();
System.out.println("The octal number is : ");
int[] oct = dto.convertToOctal(decimal);
for(int a = x - 1; a > 0; a--)
{
System.out.print(oct[a]);
}
sc.close();
}
}

```

Output:

Please enter a decimal number: 644
The octal number is : 1204

#### Using while loop

java convert decimal to octal using while loop. Here’s the example,

```import java.util.Scanner;

public class DecimalToOctalDemo
{
static int b = 1;
public static void main(String[] args)
{
int decimal, b = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a decimal number : ");
decimal = sc.nextInt();
System.out.println("The Octal number is : ");
int oct[] = new int[50];
while(decimal != 0)
{
oct[b++] = decimal % 8;
decimal = decimal / 8;
}
for(int y = b - 1; y >= 0; y--)
{
System.out.print(oct[y]);
}
sc.close();
}
}

```

Output:

Please enter a decimal number : 644
The Octal number is : 1204

##### conclusion

So this is all about how to convert decimal number to octal in java with an example.

I hope you guys have understood the concept. You can subscribe to my blog flower brackets if you haven’t already.