Binary To Decimal Conversion | C Program

Binary to Decimal in C

Let’s look at Binary to Decimal in C, we will discuss the C program for binary to decimal conversion. A decimal number can be attained by multiplying every digit of binary digit with a power of 2 and adding each multiplication outcome. The power of the integer starts from 0 and counts to n-1 where n is assumed as the overall digits of integers in binary numbers.

Ex:-  (10110)2 = 
1 * 24 + 0 * 23 + 1 * 22 + 1 * 21 + 0 * 20 16 + 0 + 4 + 2 + 0 = (22)10
Program for Binary to Decimal in C

Working:-

Binary to Decimal in C

For user input num

  • Initialize i = 0, decimal = 0
  • Extract the last digit (digit = num% 10)
  • Calculate decimal equivalent of this digit
  • Add it to decimal variable
    • decimal += digit * pow(2,i);
  • Reduce the number (num /= 10
  • increment i value

C Program to convert Binary to Decimal Number:

Code

Run
// C Program to convert binary to decimal
#include<stdio.h>
#include<math.h>

// function to convert binary to decimal
int convert(long long num)
{
    int i = 0, decimal= 0;
    
    //converting binary to decimal
    while (num!=0)
    {
        int digit = num % 10;
        decimal += digit * pow(2,i);

        num /= 10;
        i++;
    }
    return decimal;
}

// main program
int main()
{
    // long used rather than int to store large values
    // Ex : int wont store 111111111111 (12 digits) as
    // limit for int is 2147483647 (10 digits)
    long long binary;
    
    printf("Enter binary number: ");
    scanf("%lld", &binary);
    
    printf("%lld", convert(binary));
    
    return 0;
}

Output

Enter binary number: 101
5

Program for Decimal to Binary

Let us look at the program and how to convert manually the same below –

Conversion 2
conversion new

Code Decimal to Binary

Run
#include<stdio.h>


void getBinary(int dec)
{
    // since, we may need to store large binary values using long long
    long long binary = 0;
    int rem, i = 1;
    
    while(dec != 0)
    {
        rem = dec % 2;
        dec /= 2;
        binary += rem * i;
        
        // moving to next position ex: units -> tens
        i *= 10;
    }
    printf("%lld",binary);
}
 
int main()
{
    int dec = 19;
    getBinary(dec);
    return 0;
}

Output

10011

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

3 comments on “Binary To Decimal Conversion | C Program”


  • edaranagarjuna36736

    #include
    #include
    int main()
    {
    int i=0.s=0,r;
    long long n;
    printf(“Enter binary number :”);
    scanf(“%lld”,&n);
    while(n>0)
    {
    r=n%10;
    s=s+r*pow(2,i);
    n=n/10;
    i++;
    }
    printf(“Decimal number : %lld”,s);
    }


  • Sushil

    There is one mistake in Binary to Decimal
    the last Printf statement in the main() should be either typecasted or instead of putting %lld you should put %d otherwise you’re getting a garbage value as a Decimal…..
    so replace last print from main() by
    printf(“Decimal is : %lld”,(long long)convert(binary)); //Typecasted int into long long
    OR
    printf(“%d”, convert(binary));


  • Sanket

    there is no invalid state for invalid binary number
    #include

    int main()
    {
    int num, binary_val, decimal_val = 0, base = 1, rem,flag=0;

    printf(“Insert a binary num (1s and 0s) \n”);
    scanf(“%d”, &num); /* maximum five digits */

    binary_val = num;
    while (num > 0)
    {

    rem = num % 10;
    if (rem==1 || rem==0)
    {
    decimal_val = decimal_val + rem * base;
    //num/=10;
    num = num / 10 ;
    //base*=2;
    base = base * 2;
    }
    else
    {
    printf(“Invalid number”);
    flag=1;
    break;
    }

    }
    if(flag==0)
    {
    //display binary number
    printf(“The Binary num is = %d \n”, binary_val);
    //display decimal number
    printf(“Its decimal equivalent is = %d \n”, decimal_val);
    return 0;
    }
    }