Java Program to Convert Decimal to Octal

In Computer Science, generally Octal number system used to store big data values. In this post, we will develop a Java program to convert the decimal value to an octal value.

DecimalOctal
11
22
33
44
55
66
77
810
911
1012

In Octal number system 8 and 9 are not available because their base is 8.

Procedure to develop a method to convert decimal to octal,
1) Take a decimal number
2) Declare octal, remainder, multiplier, and iteration variables

// store octal value
int octal = 0; 
// store remainder in each iteration
int remainder = 0; 
// calculate multiplier value
int multiplier = 0; 
int i = 0; // iterator variable

3) Calculate the remainder variable after dividing the given number by base of octal (8).
4) Calculate the multiplier value. In each iteration multiplier = 10^i
5) Increase Octal value as, octal = octal + (remainder * multiplier)
6) Update decimal number through dividing it by 8
7) Increase the iterator variable by 1
8) Repeat 3 to 7 step until the decimal number becomes 0

import java.util.Scanner;

public class DecimalToOctal {
  private static int toOctal(int decimal) {

     // declare variables
     int octal = 0;
     int remainder = 0;
     int multiplier = 0;
     int i = 0;

     while (decimal!=0) {
        // find remainder after
        // dividing by 8
        remainder = decimal % 8;

        // calculate multiplier value
        multiplier = (int) Math.pow(10, i);

        // increase octal value
        octal += (remainder * multiplier);
        // divide decimal value by 8
        decimal /= 8;

        // increase i value by 1
        i++;
     }
     return octal;
  }

  public static void main(String[] args) {
     // declare variables
     int decimal = 0;
     int octal = 0;

     // create Scanner class object
     Scanner scan = new Scanner(System.in);

     // read inputs
     System.out.print("Enter decimal number:: ");
     decimal = scan.nextInt();

     // convert decimal to octal
     octal = toOctal(decimal);

     // display result
     System.out.println("Octal value = "
                            + octal);

     // close Scanner class object
     scan.close();
  }
}

The output for the different test-cases:-

Enter decimal number:: 8
Octal value = 10

Enter decimal number:: 10
Octal value = 12

In this program, we have used a while loop to find the sum of natural numbers in Java. While loop is a pre-test loop where the expression is evaluated then only statements are executed. It uses a test expression to control the loop. Before every iteration of the loop, the test expression is evaluated.

We can also use for loop instead of using a while loop. The for loop is also a pre-test loop, where first of all initialization expression is evaluated then the condition is checked and if the condition is true then only the statements of the for loop are executed.

import java.util.Scanner;

public class DecimalToOctal {
  private static int toOctal(int decimal) {
     // declare variables
     int octal = 0;
     for(int i=0; decimal!=0; decimal/=8, i++){
        octal += ((decimal%8) *
                  ((int)Math.pow(10, i)));
     }
     return octal;
  }

  public static void main(String[] args) {
     // declare variables
     int decimal = 0;
     int octal = 0;

     // create Scanner class object
     Scanner scan = new Scanner(System.in);

     // read inputs
     System.out.print("Enter decimal number:: ");
     decimal = scan.nextInt();

     // convert decimal to octal
     octal = toOctal(decimal);

     // display result
     System.out.println("Octal value = " + octal);

     // close Scanner class object
     scan.close();
  }
}

Decimal to Octal in Java using toOctalString() method

To convert decimal to octal value in Java the toOctalString() method is already given in the Integer class. The prototype of toOctalString() method is:-

public static String toOctalString(int i)

It returns a string representation of the integer argument as an unsigned integer in base 8.

The unsigned integer value is the argument plus 2^32 if the argument is negative; otherwise, it is equal to the argument. This value is converted to a string of ASCII digits in octal (base 8) with no extra leading 0s.

The value of the argument can be recovered from the returned string s by calling Integer.parseUnsignedInt(s, 8).

If the unsigned magnitude is zero, it is represented by a single zero character ‘0’ (‘\u005Cu0030’); otherwise, the first character of the representation of the unsigned magnitude will not be the zero character.

import java.util.Scanner;

public class NumberConversion {
  public static void main(String[] args) {

     // declare variables
     int decimal = 0;

     // create Scanner class object
     Scanner scan = new Scanner(System.in);
     // read inputs
     System.out.print("Enter decimal number:: ");
     decimal = scan.nextInt();

     // convert decimal number to octal as String
     // using toOctal() method
     String str = Integer.toOctalString(decimal);

     // convert string to number
     int octal = Integer.parseInt(str);

     // display result
     System.out.println("Octal value: "+octal);

     // close Scanner class object
     scan.close();
  }
}

Enter decimal number:: 10
Octal value: 12

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *