Convert Binary To Decimal C Program

By | December 17, 2015

C Program Convert Binary To Decimal Number

Learn How To Convert Binary To Decimal Number in C Programming Language. It is important that we should know about How A For Loop Works before getting further with the C Program Code.

A Binary Number consists of only 0 and 1 and a Decimal Number consists of values from 0 to 9. To Convert a Binary value into a Decimal Integer, a Modulus Operator (%) has to be used.

To compile this program in Linux Ubuntu, you need to type the following command:

gcc test.c -lm

Method 1: C Program For Conversion of Binary into Decimal Numbers using Function

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

int Binary_To_Decimal(int x)
{
      int decimal_number = 0, count;
      for(count = 0; x > 0; count++)
      {
            decimal_number = decimal_number + pow(2, count) * (x % 10);
            x = x / 10;
      }
      return decimal_number;
}

int main()
{
      int binary_number, result;
      printf("\nEnter A Binary Value: \t");
      scanf("%d", &binary_number);
      result = Binary_To_Decimal(binary_number);
      printf("\nDecimal Equivalent of Binary Number: \t %d\n", result);
      return 0;
}

Method 2: C Program To Convert Binary To Decimal Number using Array

#include<stdio.h>

int power_func(int c, int d)
{
      int value = 1;
      int count = 1;
      while(count <= d) 
      {
            value = value * c;
            count++;
      }
      return value;
}

int main()
{
      int bin_num[15]; 
      int digit_count, dec_num = 0; 
      int a = 0, i;
      printf("\nEnter The Number of Digits in Binary Number: \t");
      scanf("%d", &digit_count);
      printf("\nEnter Binary Digits One After Another:\n");
      for(i = 0; i < digit_count; i++)
      {
            scanf("%d", &bin_num[i]);
      }
      for(i = (digit_count - 1); i >= 0; i--) 
      {
            dec_num = (bin_num[i] * power_func(2, a)) + dec_num;
            a++;
      }
      printf("\nDecimal Equivalent of Binary Number: \t %d", dec_num);
      printf("\n");
      return 0;
}

Method 3: Convert Binary Number into Decimal in C without using Array

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

int main()
{
      int binary_number, decimal_number = 0, count;
      printf("\nEnter A Binary Value: \t");
      scanf("%d", &binary_number);
      for(count = 0; binary_number > 0; count++)
      {
            decimal_number = decimal_number + pow(2, count) * (binary_number % 10);
            binary_number = binary_number / 10;
      }
      printf("\nDecimal Equivalent of Binary Number: \t %d\n", decimal_number);
      return 0;
}

Output

C Program To COnvert Binary To Decimal Number

If you have any compilation errors or doubts in this C Program To Convert Binary To Decimal Number, let us know about in the Comment Section below.

Recommended Programs
C Program For Hexadecimal To Binary Digits Conversion
C Program To Check if a Number is Armstrong Number or Not
C Program To Convert Hexadecimal To Decimal Value
C Program To Convert Decimal Number into Octal Number
C Program For Octal Number To Decimal Number Conversion
C Program To Convert Decimal Number into Hexadecimal Number
C Program To Convert Decimal Number To Binary Value
C Program To Convert Numbers into Words
C Program To Convert Decimal into Binary, Hexadecimal and Octal Values

Let's Discuss