Recursion – java program to convert decimal to octal

Let’s learn decimal to octal java and recursion – java program to convert decimal to octal.

Recursion - java program to convert decimal to octal

Recursion – java program to convert decimal to octal

Java decimal to octal is one of the common java interview question.

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

Also read – linear search in java

// decimal to octal
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