Pages

This blog is under construction

Wednesday, December 12, 2018

C program to convert binary number to decimal number.

Example  (11001)2  =(?)10

Solution:


=11001 
=1X24 +1X23 + 0X22 +0X21 + 1X20  
=16 + 8 + 0 + 0 +1

=25

C Program:

#include<stdio.h>
#include<math.h>
void main()
   {   int n,r,s=0,p=0;
        printf("\nEnter a binary no.");
        scanf("%d",&n);
        while(n>0)
          {
            r=n%10;
            if(r!=0 && r!=1)
           {
               printf("\nInput no is not a binary number");
              exit(0);
         }
         s=s+r*pow(2,p);
         p++;
         n=n/10;
    }
    printf("\nDecimal number equivalent to input binary no. is=%d",s);
}

Output:


Enter a binary no. 11001                                                                                                                         
Decimal number equivalent to input binary no. is=25

No comments:

Post a Comment