Convert decimal number to Octal – (java api, iterative & recursive)

  • Given a decimal number (base 10), convert decimal number to octal (base 8) using java.
  • We will discuss various methods to convert decimal integer to octal.
    1. Convert decimal to octal using Integer class
      • Integer.toOctalString(int number)
      • Integer.toString(int number, int radix)
    2. Iterative program to convert decimal number to octal number.
    3. Recursive method to convert decimal number to octal number.

Methods to convert decimal integer to octal :

1.) Convert decimal to octal using Integer.toOctalString

private static String toOctalString(int number) {
 return Integer.toOctalString(number);
}

2.) Convert decimal to octal using Integer.toString

private static String toString(int number) {
 return Integer.toString(number, 8);
}

3.) Convert decimal to octal using iterative method

private static String iterative(int number) {
  StringBuilder builder = new StringBuilder();

  while (number > 0) {
   builder.append(number % 8);
   number /= 8;
  }
  return builder.reverse().toString();
}

4.) Convert decimal to octal using recursive method

private static String recursive(int number) {
 StringBuilder builder = new StringBuilder();
 
 if (number > 0) {
  String octalNumber = recursive(number / 8);
  int digit = number % 8;
  builder.append(octalNumber + digit);
 }
 return builder.toString();
}

Complete program to convert decimal number to octal

package org.learn;

import java.util.Scanner;

package org.learn;

import java.util.Scanner;

public class DecimalToOctal {

    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            System.out.printf("1. Enter decimal number : ");
            int number = scanner.nextInt();
            String octal = toOctalString(number);

            System.out.printf("2. Octal of (%d) using toOctalString method = %s\n", number, octal);

            octal = toString(number);
            System.out.printf("3. Octal of (%d) using toString method = %s\n", number, octal);

            octal = iterative(number);
            System.out.printf("4. Octal of (%d) using iterative method = %s\n", number, octal);

            octal = recursive(number);
            System.out.printf("5. Octal of (%d) using recursive method = %s\n", number, octal);
        }
    }

    private static String toOctalString(int number) {
        return Integer.toOctalString(number);
    }

    private static String toString(int number) {
        return Integer.toString(number, 8);
    }

    private static String iterative(int number) {
        StringBuilder builder = new StringBuilder();

        while (number > 0) {
            builder.append(number % 8);
            number /= 8;
        }
        return builder.reverse().toString();
    }

    private static String recursive(int number) {
        StringBuilder builder = new StringBuilder();

        if (number > 0) {
            String octalNumber = recursive(number / 8);
            int digit = number % 8;
            builder.append(octalNumber + digit);
        }
        return builder.toString();
    }
}

Program output –  convert decimal number to octal

1. Enter decimal number : 100
2. Octal of (100) using toOctalString method = 144
3. Octal of (100) using toString method = 144
4. Octal of (100) using iterative method = 144
5. Octal of (100) using recursive method = 144

1. Enter decimal number : 25
2. Octal of (25) using toOctalString method = 31
3. Octal of (25) using toString method = 31
4. Octal of (25) using iterative method = 31
5. Octal of (25) using recursive method = 31
Scroll to Top