Convert Decimal to Octal Using Stack in Java

You all are aware of Stacks. It performs two basic operations push and pop. The push operation adds an element to the top of the list, or initializing the stack if it is empty and the pop operation removes an item from the top of the list. Using these operations, we have converted a decimal number into Octal.

  1. Divide the decimal number by 8
  2. Treat the division as an integer division. Write down the remainder (in Octal).
  3. Divide the result again by 8.
  4. Treat the division as an integer division. Repeat step 2 and 3 until result is 0.
  5. The Oct value is the digit sequence of the remainders from the last to first.

 Octal   0 1 2 3 4 5 6 7 
 DECIMAL 0 1 2 3 4 5 6 7 


Convert Decimal to Octal Using Stack in Java


When you run the program, the output will be:

Enter positive integer : 15
Contents of Stack : 7 1 
Octal : 17
Enter positive integer : 9
Contents of Stack : 1 1 
Octal : 11

Post a Comment

Thank you for vising

Previous Post Next Post