C++ Online Compiler
Example: C++ program to convert decimal to binary using stack
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// C++ program to convert decimal to binary using stack #include <iostream> #include <stack> using namespace std; // The main function of the program int main() { // Step-1 Declare variables int num; stack<int> binaryStack; // Step-2 Input decimal number cout << "Enter a decimal number: "; cin >> num; // Step-3 Convert using stack to reverse bits if (num == 0) { cout << "Binary representation: 0\n"; return 0; } while (num > 0) { binaryStack.push(num % 2); num /= 2; } // Step-4 Output binary from stack cout << "Binary representation: "; while (!binaryStack.empty()) { cout << binaryStack.top(); binaryStack.pop(); } cout << endl; return 0; }
Output
Clear
ADVERTISEMENTS