Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
56 views
in JAVA by (117k points)
Explain Java Convert Decimal to Octal

Please log in or register to answer this question.

1 Answer

0 votes
by (117k points)

Certainly! Here's an example code snippet that demonstrates how to convert a decimal number to octal using Java:

public class DecimalToOctal {
    public static void main(String[] args) {
        int decimalNumber = 123; // The decimal number to convert

        String octalNumber = convertDecimalToOctal(decimalNumber);
        System.out.println("Octal equivalent of " + decimalNumber + " is: " + octalNumber);
    }

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

        while (decimalNumber > 0) {
            int remainder = decimalNumber % 8; // Get the remainder when divided by 8
            octal.insert(0, remainder); // Insert the remainder at the beginning of the octal string
            decimalNumber = decimalNumber / 8; // Update the decimal number by dividing by 8
        }

        return octal.toString();
    }
}
 

In this code, we define a class called DecimalToOctal with a main method. Inside the main method, we set the decimalNumber variable to the decimal value we want to convert to octal (in this case, 123).

The convertDecimalToOctal method takes an integer (decimalNumber) as input and returns a string representing its octal equivalent. It uses a StringBuilder named octal to build the octal number digit by digit.

Inside the while loop, we repeatedly perform the following steps until the decimalNumber becomes 0:

  1. Calculate the remainder of decimalNumber divided by 8 using the modulus operator (%).
  2. Insert the remainder at the beginning of the octal string using the insert method of StringBuilder.
  3. Update the decimalNumber by dividing it by 8.

Once the decimalNumber becomes 0, the loop terminates, and we convert the StringBuilder octal to a string using the toString method and return the result.

Finally, in the main method, we call convertDecimalToOctal with decimalNumber as an argument, store the returned octal string in the octalNumber variable, and print the result to the console.

If you run this code, it will output:

Octal equivalent of 123 is: 173
 

This means that the octal representation of the decimal number 123 is 173.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...