Home > Backend Development > C++ > body text

Decimal to multi-base conversion using stack

WBOY
Release: 2023-09-08 18:45:03
forward
1527 people have browsed it

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;
Copy after login

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

int baseNum = 2;
Copy after login

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);
Copy after login

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
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template