Let’s learn java convert decimal to octal.

## Java convert decimal to octal

decimal to octal in java is one of the frequently asked interview question. In the below example we need to convert given decimal number (base value 10) into octal number (base value 8).

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

There are two ways in decimal to octal conversion one using Integer.toOctalString() method and second using custom logic. Below is the syntax of 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 java program.

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

Here’s the program to 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