1. Introduction

In this tutorial, we are going to write a Java program to convert a decimal number to its octal equivalent. Octal representation is another numbering system that, unlike decimal, is base-8. Understanding various number bases is crucial in various fields of computer science and mathematics.

2. Program Steps

1. Define a class named DecimalToOctal.

2. Inside the class, define the main method.

3. Inside the main method, declare an int variable to represent the decimal number.

4. Use a loop to convert the decimal number to octal by repeatedly dividing it by 8 and appending the remainder to the octal string.

5. Print out the decimal number and its octal equivalent.

3. Code Program

public class DecimalToOctal { // Step 1: Define a class named DecimalToOctal

    public static void main(String[] args) { // Step 2: Define the main method

        // Step 3: Declare an int variable to represent the decimal number
        int decimal = 53;

        // Step 4: Convert decimal to octal
        String octal = "";
        int remainder;
        while (decimal > 0) {
            remainder = decimal % 8;
            octal = remainder + octal;
            decimal = decimal / 8;
        }

        // Step 5: Print the decimal number and its octal equivalent
        System.out.println("Decimal Number: " + decimal);
        System.out.println("Octal Equivalent: " + octal);
    }
}

Output:

Decimal Number: 0
Octal Equivalent: 65

4. Step By Step Explanation

Step 1: A class named DecimalToOctal is defined.

Step 2: The main method is defined within the DecimalToOctal class.

Step 3: An int variable decimal is declared to represent the decimal number 53.

Step 4: A while loop is used to convert the decimal number to its octal equivalent. The loop continues until the decimal number becomes 0. Inside the loop, we calculate the remainder of the decimal number when divided by 8, prepend it to the octal string, and then update the decimal number by dividing it by 8.

Step 5: The original decimal number (which is now 0) and its octal equivalent are printed to the console.