Tags

, , ,

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

int binary_to_decimal(int binary) /* Function to convert binary to decimal.*/
{
int decimal = 0, i = 0, reminder;
while (binary != 0)
{
reminder = binary % 10;
binary /= 10;
decimal += reminder*pow(2.0,i);
++i;
}
return decimal;
}

int decimal_to_binary(int decimal) /* Function to convert decimal to binary.*/
{
int reminder, i = 1, binary = 0;
while (decimal != 0)
{
reminder = decimal%2;
decimal /= 2;
binary += reminder*i;
i *= 10;
}
return binary;
}

Advertisements