Java Online Compiler
Example: Octal to Decimal (Manual) in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Octal to Decimal (Manual) import java.util.Scanner; // Main class containing the entry point of the program public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Step 1: Prompt user for an octal number System.out.print("Enter an octal number: "); int octalNumber = scanner.nextInt(); // Step 2: Initialize decimal value and power int decimalNumber = 0; int power = 0; // Represents 8^0, 8^1, 8^2, etc. int tempOctalNumber = octalNumber; // Use a temporary variable for calculation // Step 3: Loop through each digit of the octal number while (tempOctalNumber != 0) { // Get the last digit of the octal number int digit = tempOctalNumber % 10; // Add the decimal equivalent of the digit to the total // digit * (8 ^ power) decimalNumber += digit * Math.pow(8, power); // Remove the last digit from the octal number tempOctalNumber /= 10; // Increment the power for the next digit power++; } // Step 4: Display the result System.out.println("Decimal equivalent: " + decimalNumber); scanner.close(); } }
Output
Clear
ADVERTISEMENTS