Ad

Thursday, July 25, 2013

Conversion from Binary to Decimal

Sometimes  we need to convert a binary number into decimal number. The following code will help to convert a number from binary to decimal. Let's see and use it.................

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

 int main()
 {

     char binary[65];
     int len, decimal, power, i;

     printf("Enter the binary number: ");
     scanf("%s", binary);

     decimal = 0;
     len = strlen(binary);

     power = len - 1;

     for(i = 0; i < len; i++)
     {
         decimal +=(binary[i]-'0')*pow(2, power);
         power--;
     }
     printf("Decimal value is %d\n", decimal);

     return 0;
 }

0 comments:

Post a Comment