Embed presentation
Download to read offline
![In C Programming:
Create a program that converts a number from decimal to binary. A stack MUST be used to
complete the program. Create an implementation of a stack. An array or linked list can be used
as the underlying structure. The following stack functions MUST be implemented into the
program:
• Void pop()
• Void push(int data)
• Int top()
• Int isEmpty()
The program should take decimal input from the user, and convert it to binary, and simply print
it out.
Example: Enter a number to convert to binary: 29 11101
Solution
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define MAX 10
typedef struct stack
{
int data[MAX];](https://image.slidesharecdn.com/incprogramming-createaprogramthatconvertsanumberfromdecimal-230209005151-f56ad236/85/In-C-Programming-Create-a-program-that-converts-a-number-from-decimal-docx-1-320.jpg)
![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;
}
//-----------------------](https://image.slidesharecdn.com/incprogramming-createaprogramthatconvertsanumberfromdecimal-230209005151-f56ad236/85/In-C-Programming-Create-a-program-that-converts-a-number-from-decimal-docx-2-320.jpg)
![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](https://image.slidesharecdn.com/incprogramming-createaprogramthatconvertsanumberfromdecimal-230209005151-f56ad236/85/In-C-Programming-Create-a-program-that-converts-a-number-from-decimal-docx-3-320.jpg)

The document outlines a C programming task to create a program that converts decimal numbers to binary using a stack. It requires implementing a stack with specific functions such as push, pop, top, and isempty. The program prompts the user for a decimal number, processes it, and outputs the binary equivalent.
![In C Programming:
Create a program that converts a number from decimal to binary. A stack MUST be used to
complete the program. Create an implementation of a stack. An array or linked list can be used
as the underlying structure. The following stack functions MUST be implemented into the
program:
• Void pop()
• Void push(int data)
• Int top()
• Int isEmpty()
The program should take decimal input from the user, and convert it to binary, and simply print
it out.
Example: Enter a number to convert to binary: 29 11101
Solution
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define MAX 10
typedef struct stack
{
int data[MAX];](https://image.slidesharecdn.com/incprogramming-createaprogramthatconvertsanumberfromdecimal-230209005151-f56ad236/85/In-C-Programming-Create-a-program-that-converts-a-number-from-decimal-docx-1-320.jpg)
![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;
}
//-----------------------](https://image.slidesharecdn.com/incprogramming-createaprogramthatconvertsanumberfromdecimal-230209005151-f56ad236/85/In-C-Programming-Create-a-program-that-converts-a-number-from-decimal-docx-2-320.jpg)
![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](https://image.slidesharecdn.com/incprogramming-createaprogramthatconvertsanumberfromdecimal-230209005151-f56ad236/85/In-C-Programming-Create-a-program-that-converts-a-number-from-decimal-docx-3-320.jpg)
