Convert decimal to Binary Using Stack [Decimal to Binary Conversion ]

June 13, 2024 10 Comments » Hits : 1,120





Convert decimal to Binary Using Stack [Decimal to Binary Conversion ]


Decimal number can be converted into equivalent binary number using stack , The procedure to convert the given number into binary is described in the following video .

Program :

#include<stdio.h>
#include<conio.h>
#include<process.h>
#define MAX 10
typedef struct stack
{
 int data[MAX];
 int top;
}stack;
//---------------------
int empty(stack *s)
{
 if(s->top==-1)
      return(1);
 return(0);
}
//----------------------
int full(stack *s)
{
 if(s->top==MAX-1)
     return(1);
 return(0);
}
//-----------------------
void push(stack *s,int x)
{
 s->top=s->top+1;
 s->data[s->top]=x;
}
//-----------------------
int pop(stack *s)
{
 int x;
 x=s->data[s->top];
 s->top=s->top-1;
 return(x);
}
//------------------------
void main()
{
 stack s;
 int num;
 s.top=-1;
     printf("\nEnter decimal number:");
     scanf("%d",&num);
     while((num!=0))
     {
       if(!full(&s))
          {
          push(&s,num%2);
          num=num/2;
          }
       else
          {
          printf("\nStack overflow");
          exit(0);
          }
     }
    printf("\n");
    while(!empty(&s))
       {
       num=pop(&s);
       printf("%d",num);
       }
}

Explanation :
Step 1:

s.top=-1;
  • Initially Top = -1 .
  • It is used to create empty Stack.

Step 2 :

printf("nEnter decimal number:");scanf("%d",&num);
  • Accept Decimal Number using Scanf

Step 3:
As per video , Now we are dividing the original number by 2 and remainder is pushed onto stack , In the Second Iteration again we are dividing num by 2 and remainder is pushed onto stack . This action is carried out till number becomes 0.
Step 4 :

if(!full(&s))
          {
          push(&s,num%2);
          num=num/2;
          }
  • Push Remainder Onto Stack if Stack is not Full and number != 0

When number becomes 0 i.e Do not push elements onto stack. Now as shown in video after pushing pop all remainders one by one display it .

Incoming search terms:

  • Ashok

    well!
    the video provided here helps to understand the concept quickly! if possible provide videos in most of the programs

  • rutz

    well well infact very well video…
    adn program is simple sweet and very easy to understand…

    thanx a lot..

  • Roderick Stahoski

    I always was interested in this subject and still am, appreciate it for posting.

  • Fermin Dungee

    Hi there, just became aware of your blog through Google, and found that it’s truly informative. I’m going to watch out for brussels. I’ll appreciate if you continue this in future. Numerous people will be benefited from your writing. Cheers!

  • Lawrence Menhennett

    thanks for your thoughts on this, I felt a bit struck by this article. Thanks again!

  • Nicki Buker

    Fantastic editorial. Remember to keep up the very notable work.

  • marketing strategies for small business

    very good put up, i definitely love this website, carry on it

  • Ann McAlister

    Does your website have a contact page? I’m having a tough time locating it but, I’d like to send you an e-mail. I’ve got some recommendations for your blog you might be interested in hearing. Either way, great website and I look forward to seeing it grow over time.

    • Pritesh

      check out footer section . I have updated it…

  • Christy Dankmeyer

    Thank you your post, previously it was interesting and compelling. I discovered my way here through Google, I am going to come back once more