C Program To Convert Binary Number To Decimal Number, using While Loop

Lets write a C program to convert a number from Binary number system(base 2) to Decimal number system(base 10), using while loop.

Related Read:
while loop in C programming
Calculate Sum of Digits: C Program
C Program To Reverse a Number

Note: Binary number system can be derived by base 2 to the power of whole numbers.

Example:
etc .. 24, 23, 22, 21, 20

etc .. 24 = 16, 23 = 8, 22 = 4, 21 = 2, 20 = 1.

Expected Output for the Input

User Input:
Enter a Binary Number
1110

Output:
Decimal Equivalent of 1110 is 14

Explanation:

(23 x 1) + (22 x 1) + (21 x 1) + (20 x 0 )
= 8 + 4 + 2
= 14.

Video Tutorial: C Program To Convert Binary Number To Decimal Number, using While Loop



YouTube Link: https://www.youtube.com/watch?v=UZMuZX_gtMs [Watch the Video In Full Screen.]

Logic To Convert Binary Number To Decimal Number, using While Loop

In this C program we ask the user to enter / input a binary number. Using while loop we fetch digits of that user entered number one by one. If the fetched value is 1, then we check the place value and use pow() method to place it properly in the Binary number system. i.e., if fetched digit is from position 3, then we use pow(2, 3) to calculate its decimal equivalent value i.e., 2 x 2 x 2 = 8. And then add this value to previous value of variable dec.

Source Code: C Program To Convert Binary Number To Decimal Number, using While Loop

#include < stdio.h >
#include < math.h >

int main()
{
    long num;
    int  dec = 0, rem = 0, place = 0;

    printf("Enter a Binary Number\n");
    scanf("%ld", &num);

    printf("Decimal Equivalent of %ld is ", num);
    while(num)
    {
        rem = num % 10;
        if(rem)
        {
            dec = dec + (pow(2, place) );            
        }
        num = num / 10;
        place++;
    }
    printf("%d\n", dec);

    return 0;
}

Output 1:
Enter a Binary Number
1000
Decimal Equivalent of 1000 is 8

Output 2:
Enter a Binary Number
1111
Decimal Equivalent of 1111 is 15

We can even write the program like this:

#include < stdio.h >
#include < math.h >
int main()
{
    long num;
    int  dec = 0, rem = 0, place = 0;

    printf("Enter a Binary Number\n");
    scanf("%ld", &num);

    printf("Decimal Equivalent of %ld is ", num);
    while(num)
    {
        rem = num % 10;
        dec = dec + rem * ( pow(2, place) );
        num = num / 10;
        place++;
    }
    printf("%d\n", dec);

    return 0;
}

Output 1:
Enter a Binary Number
1011
Decimal Equivalent of 1011 is 11

Output 2:
Enter a Binary Number
10011
Decimal Equivalent of 10011 is 19

Number Systems

1. Binary Number System uses base 2 and digits 01.
2. Octal Number System uses base 8 and digits 01234567.
3. Decimal Number System uses base 10 and digits 0123456789.
4. Hexadecimal Number System uses base 16 and digits 0123456789ABCDEF.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert