You are currently viewing Best Binary to Decimal conversion solution in 2023

Best Binary to Decimal conversion solution in 2023

Code for Binary to Decimal conversion

For learning c code, students are advised to practice few basic codes, Binary to Decimal conversion is one of them.

In C code practice, this program logic plays a very important role. So I have designed this code as easy as possible for the students. They will be able to understand each and every step very easily. 

Before going for this type of program a student should go through basics of loop structure. So that they can easily go through each step with self explanation mode. 

For more code click here. For video tutorial on several topics click here.

#include <stdio.h>
int bintodec(int);
int power(int,int);


int main()
{
int bin,dec;
printf("Enter the binary form of the number(5 means 101) : ");
scanf("%d",&bin);

dec=bintodec(bin);

printf("Decimal of Binary is : %d ",dec );

    return 0;
}


int bintodec(int bin)
{
    int dec=0,bit,po=0;
    
    while(bin>0)
    {
        bit=bin%10;
        dec=dec+power(2,po)*bit;
        bin=bin/10;
        po++;
    }
    return dec;
}

int power(int p,int q)
{
    int po=1;
    for(;q>0;q--)
    {
        po=po*p;
    }
    return po;
}

Leave a Reply