write c program to convert binary number into decimal.  Both the decimal number system and the binary number system are positional number  systems. The binary system has only two numerals 0 and 1, the decimal system  has 10 numerals 0,1,2,3,4,5,6,7,8, and 9.  Let us understand the positional meaning of decimal number.

c program to convert binary number into decimal

127

The ‘1’ in 127 represents 100. The ‘2’ represents 20 and ‘7’ represents 7. summation of all these representation would be 127. The ‘1’ represents 100 because it is in 100 position similarly ‘2’ represents 20 because ‘2’  positioned at 10 and ‘7’ represents 7 because it is in unit position.

1011

The binary system has only two numerals 0 and 1. its base is 2. So, to convert the binary number 1011 to decimal, we have to find the  actual value represented by each ‘1’ and add them together. In this way 1011 in decimal would be 1×2^3 + 0x2 ^2+ 1×2^1 + 1×2^0 (11).

Now we can write simple c program to convert binary number into decimal

#include <stdio.h>

void bintodec (int a)
{
  int base = 1;
  int rem = 0;
  int dec = 0;
  while (a > 0)
  {
      rem = a % 10;
      dec = dec + rem * base;
      a = a / 10;
      base = base * 2;
  }
     printf ("%d", dec);
}

int main ()
{
  int a = 0;
  printf ("Enter binary number : ");
  scanf ("%d", &a);
  printf ("The decimal equivalent of %d is: ", a);
  bintodec (a);
  printf ("\n");
  return 0;
}

Ref:

http://mathforum.org/library/drmath/view/54311.html



Related Contents to follow