recursion java program to convert decimal to octal

Solutions on MaxInterview for recursion java program to convert decimal to octal by the best coders in the world

showing results for - "recursion java program to convert decimal to octal"
Pietro
27 Aug 2016
1import java.util.Scanner;
2public class DecimalToOctalExample
3{
4   static int octal[] = new int[50], x = 1;
5   // decimal to octal java
6   int[] convertToOctal(int oct)
7   {
8      if(oct != 0)
9      {
10         octal[x++] = oct % 8;
11         oct = oct / 8;
12         convertToOctal(oct);
13      }
14      return octal;
15   }
16   public static void main(String[] args)
17   {
18      DecimalToOctalExample dto = new DecimalToOctalExample();
19      int decimal;
20      Scanner sc = new Scanner(System.in); 
21      System.out.println("Please enter a decimal number: ");
22      decimal = sc.nextInt();
23      System.out.println("The octal number is : ");
24      int[] oct = dto.convertToOctal(decimal);
25      for(int a = x - 1; a > 0; a--)
26      {
27         System.out.print(oct[a]);
28      }
29      sc.close();
30   }
31}