static method java convert decimal to octal

Solutions on MaxInterview for static method java convert decimal to octal by the best coders in the world

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