#include <stdio.h>
#include <math.h>
int main()
{
char nos[20];
int i,fl=1,res=0,temp;
printf("Enter A Numbers : ");
gets(nos);
//check for valid binary number
for(i=0;nos[i]!='\0';i++)
{
//48 and 49 is the ascii code for 0 and 1 respectively
if(nos[i] < 48 || nos[i] > 49)
{
fl=0;
break;
}
}
if(fl)
{
strrev(nos);
puts(nos);
for(i=0;nos[i]!='\0';i++)
{
temp=nos[i]-48;
res += temp * pow(2,i);
}
printf("Binary eqivalent of %s is %d",nos,res);
}
else
{
printf("!!! *** INVALID BINARY NUMBER *** !!!");
}
}
Output
======
Enter A Numbers : 1011
Binary eqivalent of 1101 is 11