Write a C program to convert binary number to decimal using function

#include<stdio.h>
#include<math.h>
#include<string.h>

int bi2dec(char bits[20],int length);
main()
{
    int count=0,length,i=0,sum;
    char bits[20];
    printf("Enter no in Binary\n");
    gets(bits);
    while (bits[i]!='\0')
    {
        count++;
        i++;
    }
length=count;

    printf("The value of %s in decimal is = %d",bits, bi2dec(bits, length));
}
int bi2dec(char dec[],int L)
{
    int sum=0,j=0;
    for(j=0;j<L;j++)
    {
        if (dec[j]=='1')
        sum=sum+pow(2,L-1-j);

}
return sum;
}

Comments