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.
Convert Decimal to Octal Using Stack in Java
- Divide the decimal number by 8
- Treat the division as an integer division. Write down the remainder (in Octal).
- Divide the result again by 8.
- Treat the division as an integer division. Repeat step 2 and 3 until result is 0.
- The Oct value is the digit sequence of the remainders from the last to first.
Octal 0 1 2 3 4 5 6 7DECIMAL 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
Enter positive integer : 15
Contents of Stack : 7 1
Octal : 17
Enter positive integer : 9
Contents of Stack : 1 1
Octal : 11
إرسال تعليق
Thank you for vising