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.