/*binary to decimal*/
#include < stdio.h>
#include < conio.h>
#include < stdlib.h>
#include < string.h>
#include < ctype.h>
#include < process.h>
#include < math.h>
/*checking whether entered digits 0 or 1*/
void check(char * s1)
{ int i,flag=0,c=0;
for(i=0;s1[i]!='\0';i++)
{
if(s1[i]=='.')
c++;
}
for(i=0;s1[i]!='\0';i++)
{
if(s1[i]!='0' && s1[i]!='1' && s1[i]!='.')
{
flag=1;
break;
} }
if(flag==1 ||c>1)
{
printf("Error! please enter binary !");
getch();
exit(0);
}
else
return;
}
void main()
{
char binary[15];
int i,l,dec=0,integ[100],j;
clrscr();
printf("Enter Binary number:");
scanf("%s",binary);
check(binary);
l=strlen(binary);
for(i=l-1,j=0;i>=0;i--,j++)
{
/* conversion */
dec=dec+(binary[j]-48)*pow(2,i);
}
printf("\nThe binary digit is =%d",dec);
getch();
}
helpfull.....
ReplyDelete