Decimal to Multiple-Bases Conversion with Stack



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
karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know


Advertisements