Program to convert the given decimal number to binary number using stack in c.*
#include<stdio.h>
int binstack[10];
int top = -1;
int num;
int quo;
int rem;
void push(int x){
top = top +1;
binstack[top] = x;
}
void pop(){
printf("%d",binstack[top]);
top = top -1;
}
void peek(){
printf("The top most element is %d",binstack[top]);
}
void binary(){
printf("enter the number:");
scanf("%d ",&num);
while(num>1){
int rem = num%2;
if(rem != 0){
push(1);
}
else{
push(0);
}
num = num/2;
}
if(num<=1){
push(1);
}
}
void main(){
binary();
while(top!=-1){
pop();
}
}
I tried to get the binary number as the output for the corresponding decimal input but what I got all is the stucking of my program. And it sometimes gives output only when I press the ctrl+c button to terminate the program.