Decimal to Octal In Java

Hey guys!! Welcome to flower brackets blog. Today let us learn how to convert decimal to octal in java.

Decimal to Octal In Java

Below program is similar to decimal to hexadecimal conversion program. Let’s see java program for decimal to octal conversion,

Example:

import java.util.Scanner;

public class DecimalToOctal
{
   Scanner sc;
   int number;

   void getValue()
   {      
      sc = new Scanner(System.in);
      System.out.println("Please enter a decimal number: ");
      number = Integer.parseInt(sc.nextLine());
   }

   void toConvert()
   {
      String octal = Integer.toOctalString(number);
      System.out.println("The converted octal value is: " + octal);
   }

   public static void main(String[] args)
   {
      DecimalToOctal dto = new DecimalToOctal();
      dto.getValue();
      dto.toConvert();
   }
}

Output:

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


Decimal to octal conversion using static method in java

Now let us see java convert decimal to octal using a static method.

Also Read – Convert Decimal to Binary Java Example

So, let’s see an example,

import java.util.Scanner;

public class DecimalToOctal
{
   static int a = 1;
   public static void main(String[] args)
   {
      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 = convertToOctal(decimal); 
      for(int x = a - 1; x > 0; x--)
      {
         System.out.print(oct[x]);
      }
      sc.close();
   }
 
   static int[] convertToOctal(int oct)
   {
      int y[] = new int[50];
      while(oct != 0)
      {
         y[a++] = oct % 8;
         oct = oct / 8;
      }
      return y;
   }
}

Output:

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


Decimal to octal conversion using recursion in java

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

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


Java program for decimal to octal conversion using while loop

Here’s the example,

import java.util.Scanner;

public class DecimalToOctalDemo
{
   static int b = 1;
   public static void main(String[] args)
   {
      int decimal, b = 0;
      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[] = new int[50];
      while(decimal != 0)
      {  
         oct[b++] = decimal % 8;
         decimal = decimal / 8;
      } 
      for(int y = b - 1; y >= 0; y--)
      {
         System.out.print(oct[y]);
      }
      sc.close();
   }
}

Output:

Please enter a decimal number : 644
The Octal number is : 1204


Conclusion

So this is all about java program to convert decimal to octal. I hope you guys have understood the concept. You can subscribe to my blog flower brackets if you haven’t already.

Do share this article if you like.

Related Posts