print decimal to binary using stack

Solutions on MaxInterview for print decimal to binary using stack by the best coders in the world

showing results for - "print decimal to binary using stack"
Gabriele
17 Jun 2020
1#include<iostream>
2#include<stack>
3using namespace std;
4void dec_to_bin(int number) {
5   stack<int> stk;
6   while(number > 0) {
7      int rem = number % 2; //take remainder
8      number = number / 2;
9      stk.push(rem);
10   }
11   while(!stk.empty()) {
12      int item;
13      item = stk.top();
14      stk.pop();
15      cout << item;
16   }
17}
18main() {
19   int num;
20   cout << "Enter a number: ";
21   cin >> num;
22   dec_to_bin(num);
23}