C Program to Convert Binary Number to Decimal


In this example, you will learn to convert binary number to decimal manually by creating a user-defined function.


/*C Program to Convert Binary Number to Decimal*/
#include <stdio.h>
#include <math.h>
int convertBinaryToDecimal(long long n);

int main()
    long long n;
    printf("Enter a binary number: ");
    scanf("%lld", &n);
    printf("%lld in binary = %d in decimal", n, convertBinaryToDecimal(n));
    return 0;

int convertBinaryToDecimal(long long n)
    int decimalNumber = 0, i = 0, remainder;
    while (n!=0)
        remainder = n%10;
        n /= 10;
        decimalNumber += remainder*pow(2,i);
    return decimalNumber;


Enter a binary number: 110110111
110110111 in binary = 439


C Program to Convert Binary Number to Decimal

In the structure complex, we are using float datatype for variables, real and imag since our concern is to add any two complex numbers with a possible fractional value.

In the main() function, the real part and the imaginary part of the complex numbers are taken separtely using scanf().

The structures n1 and n2 are passed as an argument of function add(). The function computes the sum and returns the structure variable temp to the main() function.

The final result is printed using printf() throught the main() function.