Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C Programming: Create a program that converts a number from decimal to binary

ID: 3691223 • Letter: I

Question

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

Explanation / Answer

#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);
}
}