-
Need some advice
i have this code to convert dinary to decimal but when i enter a binary number all it does is return the number of digits i typed i.e. if i typed 110010 it should return 50 but I get 6. Any help/advice would be greatly appreciated.
Code:
#include <stdio.h>
#include <stdlib.h>
/*function prototype*/
int bin2dec(int binar4y);
int main()
{
int binary;
int decimal;
printf("The decimal equivalent = %d\n",bin2dec(binary));
getchar();
getchar();
return 0;
}
int bin2dec(int binary)
{
int b=0;
int num[10];
int i=0;
int t=0;
printf("Enter Binary Number\n");
scanf("%d", &binary);
while(binary != 0)
{
num[i] = binary % 10;
binary = binary / 10;
if(num[i] != 1 && num[i] != 0)
t = 1;
i++;
}
num[i] = 2;
binary = 1;
for(i = 0; num[i] != 2; i++)
{
b = b + num[i] * binary;
binary = binary * 2;
}
}
-
Perhaps bin2dec should return a value.