Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Decimal to Multiple-Bases Conversion with Stack
Decimal to multiple-base conversion is a common programming task where we convert a decimal number to binary, octal, hexadecimal, or any other base. Using a stack data structure makes this process efficient because stacks follow the Last-In-First-Out (LIFO) principle, which naturally reverses the remainder sequence obtained during division.
How It Works
The conversion algorithm repeatedly divides the decimal number by the target base and stores remainders in a stack. When we pop elements from the stack, we get the digits in the correct order for the converted number.
Syntax
Following is the basic algorithm for decimal to any base conversion −
Stack s = new Stack();
do {
s.Push(n % baseNum);
n /= baseNum;
} while (n != 0);
// Pop elements to get result
while (s.Count > 0) {
Console.Write(s.Pop());
}
Using Stack for Binary Conversion
Example
using System;
using System.Collections;
class Program {
public static void Main() {
int n = 45;
int baseNum = 2; // Binary conversion
Stack s = new Stack();
Console.WriteLine("Converting " + n + " to binary:");
do {
s.Push(n % baseNum);
n /= baseNum;
} while (n != 0);
Console.Write("Result: ");
while (s.Count > 0) {
Console.Write(s.Pop());
}
Console.WriteLine();
}
}
The output of the above code is −
Converting 45 to binary: Result: 101101
Using Stack for Octal Conversion
Example
using System;
using System.Collections;
class Program {
public static void Main() {
int n = 125;
int baseNum = 8; // Octal conversion
Stack s = new Stack();
Console.WriteLine("Converting " + n + " to octal:");
do {
s.Push(n % baseNum);
n /= baseNum;
} while (n != 0);
Console.Write("Result: ");
while (s.Count > 0) {
Console.Write(s.Pop());
}
Console.WriteLine();
}
}
The output of the above code is −
Converting 125 to octal: Result: 175
Using Stack for Hexadecimal Conversion
Example
using System;
using System.Collections;
class Program {
public static void Main() {
int n = 255;
int baseNum = 16; // Hexadecimal conversion
Stack s = new Stack();
Console.WriteLine("Converting " + n + " to hexadecimal:");
do {
int remainder = n % baseNum;
if (remainder < 10)
s.Push(remainder);
else
s.Push((char)('A' + remainder - 10));
n /= baseNum;
} while (n != 0);
Console.Write("Result: ");
while (s.Count > 0) {
Console.Write(s.Pop());
}
Console.WriteLine();
}
}
The output of the above code is −
Converting 255 to hexadecimal: Result: FF
Multiple Base Conversions in One Program
Example
using System;
using System.Collections;
class Program {
public static string ConvertToBase(int number, int baseNum) {
if (number == 0) return "0";
Stack s = new Stack();
string result = "";
while (number > 0) {
int remainder = number % baseNum;
if (remainder < 10)
s.Push(remainder);
else
s.Push((char)('A' + remainder - 10));
number /= baseNum;
}
while (s.Count > 0) {
result += s.Pop();
}
return result;
}
public static void Main() {
int number = 156;
Console.WriteLine("Number: " + number);
Console.WriteLine("Binary (base 2): " + ConvertToBase(number, 2));
Console.WriteLine("Octal (base 8): " + ConvertToBase(number, 8));
Console.WriteLine("Hexadecimal (base 16): " + ConvertToBase(number, 16));
Console.WriteLine("Base 5: " + ConvertToBase(number, 5));
}
}
The output of the above code is −
Number: 156 Binary (base 2): 10011100 Octal (base 8): 234 Hexadecimal (base 16): 9C Base 5: 1111
Conclusion
Using a stack for decimal to multiple-base conversion is an efficient approach because the LIFO nature of stacks naturally handles the digit ordering. This method works for any base from 2 to 36, making it a versatile solution for number system conversions.
