Answer to Question #237051 in C for Smiley

Question #237051


Using the operations of stack using linked list convert a decimal number to binary. 


1
Expert's answer
2021-09-14T18:18:07-0400
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
int Stack_Empty(int first, int stack_arr[]);
void push(int m, int *first, int arr[]);
int pop(int *first, int arr[]);
void Decimal_Binary(int n);


int main()
{
        int n;
        printf("Enter an integer : ");
        scanf("%d",&n);
        printf("It's binary equivalent is : ");
        Decimal_Binary(n);
        return 0;


}
void Decimal_Binary(int n)
{
        int stack[MAX], first=-1, rem;
        while(n!=0)
        {
                rem = n%2;
                push(rem, &first, stack);
                n/=2;
        }
        while(first!=-1)
                printf("%d", pop(&first, stack));
        printf("\n");
}
void push(int m, int *first, int arr[])
{
        if(*first == (MAX-1))
                printf("Stack Overflow\n");
        else
        {
                *first=*first+1;
                arr[*first] = m;
        }
}


int pop(int *first, int arr[])
{
        int m;
        if(*first == -1)
        {
                printf("Stack Underflow\n");
                exit(1);
        }
        else
        {
                m = arr[*first];
                *first=*first-1;
        }
        return m;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS