Use app×
QUIZARD
QUIZARD
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
95 views
in JAVA by (178k points)
Explain Java Convert Octal to Decimal

Please log in or register to answer this question.

1 Answer

0 votes
by (178k points)

Certainly! I can help you with that. Here's an example code snippet in Java that demonstrates how to convert an octal number to its decimal equivalent:

import java.util.Scanner;

public class OctalToDecimalConverter {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter an octal number: ");
        String octalNumber = scanner.next();

        int decimalNumber = convertOctalToDecimal(octalNumber);
        System.out.println("Decimal equivalent: " + decimalNumber);
    }

    public static int convertOctalToDecimal(String octalNumber) {
        int decimalNumber = 0;
        int power = 0;

        // Iterate over each digit in reverse order
        for (int i = octalNumber.length() - 1; i >= 0; i--) {
            char digit = octalNumber.charAt(i);

            // Convert the digit character to its decimal value
            int decimalValue = Character.getNumericValue(digit);

            // Multiply the decimal value with the corresponding power of 8 and add to the result
            decimalNumber += decimalValue * Math.pow(8, power);

            power++;
        }

        return decimalNumber;
    }
}
 

In this code, we take input from the user in the form of an octal number using the Scanner class. The convertOctalToDecimal method takes the octal number as a string and converts it to a decimal number using the power of 8.

We iterate over each digit of the octal number from right to left. We use the charAt method to get the individual digit character and then convert it to its decimal value using Character.getNumericValue. We then multiply the decimal value with the corresponding power of 8 (starting from 0) and add it to the decimalNumber variable.

Finally, we display the decimal equivalent of the octal number to the user.

You can run this code and enter an octal number, and it will output its decimal equivalent.

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

...