C program to Convert Decimal number into Binary using Stack

A stack is a linear data structure and follows the last in first out rule, in this tutorial, we will learn how to convert a decimal number into binary using stack.

Also read: C program to find factorial of Large Numbers

Let us first see what a stack is, a stack can be imagined as a long container and data is stored in it one by one and one on one, see the below picture to understand properly.

C program to Convert binary to decimal using stack

We can represent stack using arrays, for example, we have following data {2, 3, 12, 6} in our array then it will be stored in a stack as shown below.

Stack using Arrays

Now we will write a C program to create a stack using arrays

Below program will consist of following functions

  1. push() — To insert an item in the stack.
  2. pop() — To remove the element from the top.
  3. empty() — to check whether the stack is empty or not
  4. full() — to check stack overflow.
  5. display() — to display the stack.
#include<stdio.h> #define SIZE 10 int stack[SIZE]; int top=-1; int empty(); int full(); void push(int x); void pop(); void display(); int main(){ // code for conversion will come here } int empty(){ if(top==-1){ return 1; } else{ return 0; } } int full(){ if(top==SIZE){ return 1; } else{ return 0; } } void push(int x){ if(full()){ printf("Stack overflow!!"); } else{ top++; stack[top] = x; } } void pop(){ if(empty()){ printf("Stack empty"); } else{ top--; } } void display(){ int i; for(i=top;i>=0;i--){ printf("\%d ",stack[i]); } }

Now let us see how we can convert a decimal number into binary by using stack see the below image to understand

Decimal to binary using stack

We will add the below code into our main function to convert the decimal number into binary.

int main(){ int n,remainder; printf("Enter a decimal number:"); scanf("\%d",&n); while(n!=0){ remainder = n\%2; n = n/2; push(remainder); // inserting in stack } display(); // displaying the stack elements }

Combining the above codes, we will get the full C program to convert decimal to binary.

#include<stdio.h> #define SIZE 10 int stack[SIZE]; int top=-1; int empty(); int full(); void push(int x); void pop(); void display(); int main(){ int n,remainder; printf("Enter a decimal number:"); scanf("\%d",&n); while(n!=0){ remainder = n\%2; n = n/2; push(remainder); } display(); } int empty(){ if(top==-1){ return 1; } else{ return 0; } } int full(){ if(top==SIZE){ return 1; } else{ return 0; } } void push(int x){ if(full()){ printf("Stack overflow!!"); } else{ top++; stack[top] = x; } } void pop(){ if(empty()){ printf("Stack empty"); } else{ top--; } } void display(){ int i; for(i=top;i>=0;i--){ printf("\%d ",stack[i]); } }

Output of the above program:

Output of C program to covert decimal to binary

Thank you for reading

Tweet your queries and feedback to @PsychoCodes or leave a message on our Facebook page. You can also comment your questions below.

Also, don’t forget to subscribe to our Newsletter.

If you like this article, then please share it and help us grow.


Preorder and Postorder Traversal of binary tree in Python
02 September 2018

Binary Tree in Python
02 September 2018

Image Sharpening by High Pass Filter using Python and OpenCV
17 August 2018

Explaining Register variables in C with examples
17 August 2018

C program to generate all combinations of N-Bit Binary String
10 July 2018

Data Autosave System using PHP, MySQL and AJAX
06 July 2018

Понравилась статья? Поделиться с друзьями:
Добавить комментарий