C++ Programming Code Examples C++ > Data Structures Code Examples program to Convert a Decimal Number to Binary Number using Stacks program to Convert a Decimal Number to Binary Number using Stacks This C++ program, using a stack, displays the bits of a binary number when the corresponding decimal number is entered as input. #include <iostream> #include <stdio.h> #include <conio.h> using namespace std; struct node { int data; node *next; }*top = NULL, *p = NULL, *np = NULL; int x; void push(int n) { np = new node; np->data = n; np->next = NULL; if (top == NULL) { top = np; } else { np->next = top; top = np; } } int pop() { if (top == NULL) { cout<<"underflow\n"; } else { p = top; top = top->next; x = p->data; delete(p); return(x); } } int main() { int n, a; cout<<"enter the decimal number\n"; cin>>n; while (n > 0) { a = n % 2; n = n / 2; push(a); } p = top; cout<<"resultant binary no:"; while(true) { if (top != NULL) cout<<pop()<<"\t"; else break; } getch(); }