Wednesday, October 29, 2014

Decimal to Binary Conversion using Stack in C++

#include<iostream.h>
#include<conio.h>
class stack{
private:
int top,max;
int *stackarr;
public:
stack()
{
cout<<"\nEnter the size of stack: ";
cin>>max;
top=-1;
}
void push(int n)
{
if(top==max)
cout<<"\nStack is full\n";
else
top=top+1;
stackarr[top]=n;
}
int pop()
{
if(top==-1)
cout<<"\nStack is empty\n";
else
top=top-1;
return stackarr[top+1];
}
void decimal(int num)
{
cout<<"\n\nThe decimal to binery convertion of "<<num<<" is Below.\n ";
   for(num;num>0;num=num/2)
  {  push(num%2);       }
while(top!=-1)
  { cout<<pop()<<" ";   }
}
};
void main()
{
clrscr();
int value=0;
stack obj;
cout<<"\nEnter a number in Decimal notaion : ";
cin>>value;
obj.decimal(value);
getch();
}

Share this

0 Comment to "Decimal to Binary Conversion using Stack in C++"