codescracker
c

C Program to Convert Binary to Decimal



« Previous Program Next Program »

In this tutorial, we will learn about how to create a program in C that converts any given binary number (by user at run-time) to its equivalent decimal value. At last we have also created the same purpose program using user-defined function named BinToDec().

But before going through the program, if you are not aware of

then refer to Binary to Decimal conversion step by step process. Now let's move on to the program.

Binary to Decimal in C

To convert binary number to decimal number in C programming, you have to ask from user to enter the binary number and then convert it into decimal number as shown in the following program:

// Binary to Decimal in C
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
    int binnum, decnum=0, i=0, rem;
    printf("Enter any binary number: ");
    scanf("%d", &binnum);
    while(binnum!=0)
    {
        rem = binnum%10;
        decnum = decnum + rem*pow(2,i);
        i++;
        binnum = binnum/10;
    }
    printf("\nEquivalent Decimal Value = %d", decnum);
    getch();
    return 0;
}

As the above program was written under Code::Blocks IDE, therefore after successful build and run, you will get the following output. This is the first snapshot of the sample run:

c program convert number from binary to decimal

Now supply any binary number say 101110 and press ENTER key to see its equivalent value in decimal number system as shown in the second snapshot of the sample run given below:

binary to decimal conversion in c

Program Explained

without pow() Function

Now let's create the same program, but this time without using any pow() function of math.h library. The question is, Write a program in C that converts any given binary number to decimal number without using any pow() function. The answer to this question is:

// Binary to Decimal in C without pow() Function
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int main()
{
    int binnum, decnum=0, i=1, rem;
    printf("Enter any binary number: ");
    scanf("%d", &binnum);
    while(binnum!=0)
    {
        rem = binnum%10;
        decnum = decnum + (rem*i);
        i = i*2;
        binnum = binnum/10;
    }
    printf("\nEquivalent Decimal Value = %d", decnum);
    getch();
    return 0;
}

Here is the final snapshot of the sample run of above program:

c binary to decimal without pow function

Here we have replaced pow(2, i) (0 as initial value of i) with i (1 as initial value of i) and i++ with i=i*2. Therefore, here in this case, we will get:

Here we are getting the same values in case of pow(2, i), that are:

using User-defined Function

Now let's create another program in C that using user-defined function named BinToDec() to convert any given binary number into its equivalent decimal value as shown in the program given below:

// Binary to Decimal in C using Function
// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int BinToDec(int bin);
int main()
{
    int binnum, decnum;
    printf("Enter any binary number: ");
    scanf("%d", &binnum);
    decnum = BinToDec(binnum);
    printf("\nEquivalent Decimal Value = %d", decnum);
    getch();
    return 0;
}
int BinToDec(int bin)
{
    int dec=0, i=1, rem;
    while(bin!=0)
    {
        rem = bin%10;
        dec = dec + (rem*i);
        i = i*2;
        bin = bin/10;
    }
    return dec;
}

Here is the final snapshot of the sample run:

c binary to decimal using function

Here we have used a function named BinToDec() that takes one argument as binary number. Inside the function we have converted the given binary number to decimal number. The decimal number holds inside a variable dec. And we have returned the value of dec to the function. Therefore inside the main() function, we have initialized the returning value of function BinToDec() to a variable decnum that holds the value of dec of BinToDec() function. Finally print the value of decnum as output that will be the equivalent decimal value of given binary number.

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker