Written by Sai Sameera Thota on February 22, 2013 in C > Programming

C Program to Convert Binary to Decimal

GET ALERTS:

Get our Latest updates delivered to your mailbox!

This article provides Source code and explanation for Conversion of given Binary Number to Decimal Number. A Binary Number is a Number which will be in the form of 0’s and 1’s.

For example take a Binary digit: 110101. The corresponding Decimal Number can be given by the expression: 1*(2^5) + 1*(2^4) + 0*(2^3) + 1*(2^2) + 0*(2^1) + 1*(2^0) => 32 + 16 + 0 + 4 + 0 + 1. Answer is 53.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
      int rem, p=0, sum=0;
      long n;
      printf("Enter a Binary Number (0's and 1's) : ");
      scanf("%ld",&n);
      while(n>0)
      {
           rem = n%10;   
           if(rem > 1)
           {
                printf("\nInvalid Binary Number");
                getch();
                exit(0);
           }
           else
           {  
                sum = sum + (rem * pow(2,p));
                p++;
                n=n/10;
           }
           
      }
      printf("\nEquivalent Decimal Number = %d\n",sum);
      getch();
}                   

Output:
bin_dec

{ 1 comment… read it below or add one }

akshay November 25, 2013 at 1:02 pm

Very nice and simple code.

Reply

Leave a Comment

Previous post:

Next post: