Software & Finance Forums

### Author Topic: Binary to Decimal Conversion in Turbo C  (Read 1063 times)

#### kathir

• Sr. Member
• Posts: 283
##### Binary to Decimal Conversion in Turbo C
« on: January 23, 2012, 12:49:16 am »
Here is the Binary to Decimal Conversion program in Turbo C

Reference:

http://www.softwareandfinance.com/Turbo_C/B2D.html

http://www.softwareandfinance.com/Turbo_C/D2B.html

Code:
```//Source Code Listing

#include <stdio.h>
#include <conio.h>
#include <dos.h>

long ConvertDecimal2Binary(long dec)
{
long bin = 0, pos = 1;
while(dec > 0)
{
bin = bin + (dec % 2) * pos;
dec = dec / 2;
pos *= 10;
}
return bin;
}

long ConvertBinary2Decimal(long bin)
{
long dec = 0, pos = 0;
long factor = 1;

while(bin > 0)
{
if( (bin % 10) == 1)
{
dec += factor;
}
bin /= 10;
pos++;
factor = factor * 2;
}

return dec;
}

int main()
{
for(long i = 0; i < 128; i++)
{
if(i > 16)
i += 7;
long bin = ConvertDecimal2Binary(i);
long dec = ConvertBinary2Decimal(bin);
printf("\n%3ld = %08ld = %3ld", i, bin, dec);
}
printf("\n\n");
return 0;
}

```

Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.