Following C program would demonstrate binary to decimal conversion. In this program user would be asked to input the binary number (0s and 1s) and then the program would convert it into the equivalent decimal number. We have created a user defined function binary_decimal() in this program, specifically for the conversion purpose.

/* Description: C program to convert the input binary * number to the equivalent decimal number using user * defined function. * Written by: Chaitanya Published on: beginnersbook.com */ #include <stdio.h> int binary_decimal(int num) { int binary_num, decimal_num = 0, base = 1, rem; binary_num = num; while (num > 0) { rem = num % 10; decimal_num = decimal_num + rem * base; num = num / 10 ; base = base * 2; } return decimal_num; } void main() { int num =1010111, decimal_output; printf("Enter a binary number: \n"); //scanf("%d", &num); decimal_output = binary_decimal(num); printf("Decimal equivalent of %d is: %d \n",num, decimal_output); }

**Output:**

Enter a binary number: 1010111 Decimal equivalent of 1010111 is: 87

## Leave a Reply