Saturday, January 12, 2013

Convert binary to decimal


Convert

Convert binary to decimal.
Input 101, will be output as 5

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

int main()
{
   long int y, num, sum,k;
   char x[8];
   
   // Harusnya input terlebih dahulu, jgn strlen dulu
   scanf("%s",x); fflush(stdin);

   
   sum = 0;
   k=strlen(x);
   
   for (y=0; y<k; y++)
   {
       num=x[y]-'0';
  sum=sum + powl(2,num);
   }

   printf("%d", sum);

   getchar();

   return 0;
}

No comments: