it runs but converts wrong..
it convert 101101 as 1061.. which should be 45.
can someone fine error?
Code:/******************************************************************************** * Description of The Program: Program ask user for input(binary number) then * change binary number to decimal number *************************************************************************************/ /* Header file used*/ #include <stdio.h> /* Main Function */ main() { /* Declarations */ int binaryinput; /* Input */ int i = 2; /* base */ int power = 0; /* set power to 0 */ int sum = 0; /* sum of temporary result */ int temp = 1; /* Temporary result */ int p; /* binary number */ int storebin; /* store input */ /* Get Input From User*/ printf("Enter a binary number: "); scanf("%d", &binaryinput); storebin = binaryinput; if(binaryinput == 0) /*If input is 0 then decimal number is 0 */ printf("binaryinput = 0"); else /* Else convert to decimal */ { while (binaryinput != 0) { if ((binaryinput%10) == 1) /* Check remainder is 1 */ { if(power == 0) /* if power = 0 then result is 1*/ temp = 1; else /* else calculate the power */ { for(p = 1; p <= power; p++) temp *= i; /* Calculate 2^power */ } sum += temp; /* Add temporary results */ power++; /* find 2^power+1 */ binaryinput = binaryinput/10; /* go to next digit */ } else { power++; binaryinput = binaryinput/10; /* go to next digit */ } } binaryinput+=sum; /* Calculate sum of temporary result */ /* Out put */ printf("The decimal number for binary number %d is = %d .\n", storebin, binaryinput ); } }



LinkBack URL
About LinkBacks


