Home  >  Article  >  Backend Development  >  Decimal to multi-base conversion using stack

Decimal to multi-base conversion using stack

WBOY
WBOYforward
2023-09-08 18:45:031474browse

Decimal to multi-base conversion using stack

For multi-radix conversions, set a variable and add the base to be calculated.

Here, for our example, I set the variable baseNum to 2 -

int baseNum = 2;

Similarly, if you want base 8, set the above to -

int baseNum = 2;

You can also get the above variable values ​​as user input.

After getting the value, set up a stack and get the value -

Stack s = new Stack();
do {
s.Push(n % baseNum);
n /= baseNum;
} while (n != 0);

After using the stack, pop the element. This will give you the results.

Assuming the number n is 45, the binary result will be -

Result...
101101

The above is the detailed content of Decimal to multi-base conversion using stack. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete