Write a program in C that converts a binary string to decimal. This is what I ha
ID: 3783869 • Letter: W
Question
Write a program in C that converts a binary string to decimal.
This is what I have now but it is not working
Convert a binary number to an unsigned integer Unexpected characters are ignored. Only 1's and 0' s are considered to be valid. a10c1 should convert to 5 Oparam binary Binary number as a string of 1 s and '0's. Creturns unsigned in t result unsigned int bin2dec (const char *binary) int bin while (*binary 'le'){ if( *binary 48) bin+ if( *binary 49) bin+ 1; binary++ printf ("bintis %d bin unsigned int res; int rem; int po while bin 0) rem bin 9610 bin 10; res rem*pow pow* 2; return res;Explanation / Answer
#include <stdio.h>
#include <conio.h>
#include <math.h>
long binaryToDec(long num)
{
int remainder;
long result = 0, i=0;
while(num != 0)
{
remainder = n%10;
num = num/10;
result = result + (remainder*pow(2,i));
++i;
}
return result;
}
int main()
{
long number, answer;
scanf("%ld", &number);
answer = binToDecimal(number);
printf("%d ", answer);
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.