Given a Binary value the task is to convert it into Decimal value.
To convert Binary value to Decimal value we multiple each bit from right to left with 2 raised to the power starting from 0 and increasing towards left and then sum all the values.
Example 1: Consider a Binary value 111001. 1*25+1*24+1*23+0*22+0*21+1*20 = 32+16+8+0+0+1 = 57.
Example 2: Consider a Binary value 101. 1*22+0*21+1*20 = 4+0+1 = 5.
C
#include <stdio.h>
#include <math.h>
int main()
{
int r=0,bin=0;int dec=0;int pos=0;
printf("Binary Value: ");
scanf("%d",&bin);
while(bin>0)
{
r=bin%10;
dec=dec+(r*(int)pow(2,pos));
pos++;
bin=bin/10;
}
printf("Decimal Value: %d",dec);
return 0;
}C