Octal To Decimal Algorithm
The Octal to Decimal Algorithm is a technique used to convert an octal number (a base-8 numeral system) into its equivalent decimal number (a base-10 numeral system). Octal numbers use digits ranging from 0 to 7, making it a convenient representation for digital systems, especially in computer programming and communication with digital systems. The process of converting an octal number to a decimal number involves multiplying each digit in the octal number by the power of 8, corresponding to its position or place value, and then summing up these products to obtain the final decimal number.
To illustrate the algorithm, let's take an example: convert the octal number 1274 to its decimal equivalent. Starting from the rightmost digit (4), multiply it by 8 raised to the power of 0 (as it's the rightmost digit, its position is 0). Then, multiply the next digit (7) by 8 raised to the power of 1, and so on until each digit is multiplied by the corresponding power of 8. Lastly, sum up all these calculated products: (1 * 8^3) + (2 * 8^2) + (7 * 8^1) + (4 * 8^0) = 512 + 128 + 56 + 4 = 700. Thus, the decimal equivalent of the octal number 1274 is 700. This algorithm can be applied to any octal number to convert it into its corresponding decimal representation, allowing for seamless communication and manipulation of data in different numeral systems.
package Conversions;
import java.util.Scanner;
/**
* Converts any Octal Number to a Decimal Number
*
* @author Zachary Jones
*
*/
public class OctalToDecimal {
/**
* Main method
*
* @param args
* Command line arguments
*/
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Octal Input: ");
String inputOctal = sc.nextLine();
int result = convertOctalToDecimal(inputOctal);
if (result != -1)
System.out.println("Result convertOctalToDecimal : " + result);
sc.close();
}
/**
* This method converts an octal number to a decimal number.
*
* @param inputOctal
* The octal number
* @return The decimal number
*/
public static int convertOctalToDecimal(String inputOctal) {
try {
// Actual conversion of Octal to Decimal:
Integer outputDecimal = Integer.parseInt(inputOctal, 8);
return outputDecimal;
} catch (NumberFormatException ne) {
// Printing a warning message if the input is not a valid octal
// number:
System.out.println("Invalid Input, Expecting octal number 0-7");
return -1;
}
}
}