Decimal to Multiple-Bases Conversion with Stack

CsharpProgrammingServer Side Programming

For multiple-base conversions, set a variable and add the base you want to calculate.

Here, for our example, I have set the variable baseNum as 2 −

int baseNum = 2;

In the same way, if you want base 8, then set the above as −

int baseNum = 2;

You can also get the above variable value as user input.

After getting the value, set a stack and get the values −

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

After using the stack, pop out the elements. That would give you the result.

Let’s say the number n is 45, then the result in binary would be −

Result...
101101
raja
Published on 14-Aug-2018 08:21:18
Advertisements