Binary to Decimal in C

Nikhil Shah

Newbie
Joined
Jun 30, 2022
Messages
7
Reaction score
0
Code for switching paired over completely to decimal involving stack in C. I have utilized the stack to store the aggregate after convertion and jumped out just the top component from a stack that contains the aggregate. If it's not too much trouble, recommend any improvement. Also, Prior to attempting this code I have done some research on the web and read two or three articles on decimal to binary in c to grasp the idea in a superior manner, my sources of information: https://www.scaler.com/topics/decimal-to-binary-in-c/ and https://en.wikipedia.org/wiki/Binary-coded_decimal

Code:
#include<stdio.h>
#include<conio.h>
#define MAX 100
int stack[MAX];
int top=-1;
int num;
void push();
void pop();
main()
{
    printf("Enter the binary number: ");
    scanf("%d",&num);
    push();
    pop();
}
void push()
{
    int rem;
  
        int dec_value = 0;
        int base = 1;
        int temp = num;
        while(temp)
        {
            int last_digit = temp % 10;
            temp = temp / 10;
            dec_value += last_digit * base;
            base = base * 2;
              
        if(top>=MAX)
        {
            printf("\nSTACK OVERFLOW!");
        }
        else
        {
            top++;
            stack[top]=dec_value;
        }
    }
}
void pop()
{
    int i;
    printf("Its decimal form: ");
    printf("%d",stack[top]);
    if(top<0)
    {
        printf("\nStack is empty!");
    }
}

Could someone, please clarify in detail?
 
Back
Top