Binary to Decimal Conversion Program In C Programming

Example Program For Binary to Decimal Conversion

/*  Example Program For Binary to Decimal In C Programming Language
    little drops @ thiyagaraaj.com

    Coded By:THIYAGARAAJ MP             */

// Header Files
#include<stdio.h>
#include<conio.h>

int to_decimal(int);
//Main Function
int main()
{
    //Varibale Declaration
    int n;

    printf("Enter the Binary Number:");
    scanf("%d",&n);

    printf("Decimal Numebr : %d",to_decimal(n));

    // Wait For Output Screen
    getch();

    //Main Function return Statement
    return 0;
}

// Function for Binary to Decimal
int to_decimal(int num)
{
    int sum=0,i=0;
    while(num!=0)
    {
    sum=sum+(num%10)*pow(2,i++);
    num/=10;
    }
    return sum;
}

Sample Output:

Enter the Binary Number:1010
Decimal Numebr : 10

Enter the Binary Number:1000
Decimal Numebr : 8

Top Pages