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:
1 | gcc test.c -lm |
Method 1: C Program For Conversion of Binary into Decimal Numbers using Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #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

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.