Java Online Compiler
Example: Decimal to Octal (Manual) in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Decimal to Octal (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 a decimal number System.out.print("Enter a decimal number: "); int decimalNumber = scanner.nextInt(); // Step 2: Initialize octal value and multiplier int octalNumber = 0; int i = 1; // Multiplier to place remainders at correct positions (1, 10, 100, etc.) int tempDecimalNumber = decimalNumber; // Use a temporary variable for calculation // Step 3: Loop until the decimal number becomes 0 while (tempDecimalNumber != 0) { // Get the remainder when divided by 8 int remainder = tempDecimalNumber % 8; // Add the remainder to the octal number, multiplied by its place value octalNumber += remainder * i; // Divide the decimal number by 8 tempDecimalNumber /= 8; // Multiply 'i' by 10 to shift position for the next remainder i *= 10; } // Step 4: Display the result System.out.println("Octal equivalent: " + octalNumber); scanner.close(); } }
Output
Clear
ADVERTISEMENTS