Static method – java convert decimal to octal

Let’s learn static method – java convert decimal to octal.

Static method – java convert decimal to octal

For this first user enters a decimal number using nextInt() method of Scanner class.

static method - java convert decimal to octal

Then this number is stored in integer variable decimal. Now this variable decimal is passed as a parameter to convertToOctal() method. Now in convertToOctal() method oct is divided by 8.

After dividing oct by 8 store the remainder. Now we have to repeat previous steps until number is divided.

In the last step using for loop print the reverse of the remainder we got while dividing by 8 on the console.

This reversed octal number is equivalent to decimal number. Here’s how to write static method convert a decimal number to octal number.

import java.util.Scanner;
public class DecimalToOctal
   static int a = 1;
   public static void main(String[] args)
      int decimal;
      Scanner sc = new Scanner(;   
      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--)
   static int[] convertToOctal(int oct) 
      int y[] = new int[50];
      while(oct != 0)
         y[a++] = oct % 8;
         oct = oct / 8;
      return y;


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

Also read – java overview