C Program to Convert Binary to Decimal

Write a C program to convert binary number into decimal number. This C program accepts binary number from user and converts it into a decimal equivalent number using while loop construct in C programming language.
/* C program to convert binary number into decimal */

#include <stdio.h>

int main()
{
 int  num, binary, decimal = 0, base = 1, rem;

 printf("\n Enter a binary number:- ");

        scanf("%d", &num);

        binary = num;

        while (num > 0)
        {
            rem = num % 10;
            decimal = decimal + rem * base;
            num = num / 10 ;
            base = base * 2;
        }

        printf("\n The Binary number is %d", binary);
        printf("\n Converted decimal equivalent is %d \n", decimal);

        return 0;
}

Output:

Enter a binary number:- 10101001
The Binary number is = 10101001
Converted decimal equivalent is = 169

Comments