Decimal to Octal Conversion in Java

In this article, you will learn how to write the Java logic for Decimal to Octal conversion. The meaning of decimal to-octal conversion is converting a number from its decimal representation (base 10) to its octal representation (base 8).

Decimal Number:

In the decimal number system, we use 10 digits (0-9) to represent numbers. Each digit’s position in a decimal number represents a power of 10. For example, in the number 286, the digit 8 is in the tens place, representing 8 * 10^1, and the digit 6 is in the units place, representing 6 * 10^0.

Octal Number:

In the octal number system, we use 8 digits (0-7) to represent numbers. Each digit’s position in an octal number represents a power of 8. The rightmost digit represents 8^0, the next digit to the left represents 8^1, the next digit represents 8^2, and so on.

Decimal to Octal Conversion Process

Converting a decimal number to an octal involves dividing the decimal number by 8 repeatedly and recording the remainders until the quotient becomes 0. The remainders are then read in reverse order to obtain the octal representation of the original decimal number.

For example, the decimal number 79 can be converted to octal as follows:

79 divided by 8 gives a quotient of 9 and a remainder of 7
9 divided by 8 gives a quotient of 1 and a remainder of 1
1 divided by 8 gives a quotient of 0 and a remainder of 1

Reading the remainders in reverse order, we get the octal representation: 117.

Decimal to Octal Conversion Java Programs

Here are a few different solutions for converting a decimal number to its octal equivalent in Java:

1. Using Integer.toOctalString()

In this solution, the Integer.toOctalString() method is used to convert the decimal number to its octal representation. The method takes an integer as input and returns a string representing the octal value.

package com.javacodepoint.conversion;

import java.util.Scanner;

public class DecimalToOctal {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.print("Enter a decimal number: ");
		int decimal = scanner.nextInt();
		String octal = Integer.toOctalString(decimal);
		System.out.println("Octal equivalent: " + octal);
	}

}

OUTPUT:

Enter a decimal number: 32
Octal equivalent: 40

2. Using a loop

In this solution, a loop is used to convert the decimal number to an octal. The algorithm repeatedly divides the decimal number by 8 and appends the remainder to a string. The loop continues until the decimal number becomes 0.

package com.javacodepoint.conversion;

import java.util.Scanner;

public class DecimalToOctal2 {

	public static String convertDecimalToOctal(int decimal) {
		StringBuilder octal = new StringBuilder();

		while (decimal > 0) {
			int remainder = decimal % 8;
			octal.insert(0, remainder);
			decimal = decimal / 8;
		}

		return octal.toString();
	}

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.print("Enter a decimal number: ");
		int decimal = scanner.nextInt();
		String octal = convertDecimalToOctal(decimal);
		System.out.println("Octal equivalent: " + octal);
	}

}

OUTPUT:

Enter a decimal number: 20
Octal equivalent: 24

3. Using recursion

In this solution, recursion is used to convert the decimal number to an octal. The algorithm recursively divides the decimal number by 8 and concatenates the remainder to the result of the recursive call.

package com.javacodepoint.conversion;

import java.util.Scanner;

public class DecimalToOctal3 {

	// Recursive method
	public static String convertDecimalToOctal(int decimal) {
		if (decimal == 0) {
			return "0";
		} else {
			return convertDecimalToOctal(decimal / 8) + (decimal % 8);
		}
	}

	// main method
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.print("Enter a decimal number: ");
		int decimal = scanner.nextInt();
		String octal = convertDecimalToOctal(decimal);
		System.out.println("Octal equivalent: " + octal);
	}

}

OUTPUT:

Enter a decimal number: 16
Octal equivalent: 020

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework

Leave a Reply

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