C Program to Convert Binary Number into Decimal Number.

#include<stdio.h>
#include<math.h>
void main()
{
int n, sum=0, p=0,r,num, rem,check;
printf(“enter the value of n\n”);
scanf(“%d”,&n);
num=n;
while(num!=0)                                               //this part is to check whether the input is binary or not
{
rem=num%10;
if(rem>1)
{
printf(“\nthe number is not binary plz enter binary number only\n”);
check=1;
exit(0);
}

else
{
num=num/10;
}

}
while(n!=0)                                                 //convertion part
{
r=n%10;
sum=sum+pow(2,p)*r;                    //pow() fucntion is included in header file math.c
n=n/10;
p++;

}
printf(“the equivalent decimal number is %d”,sum);
}

OUTPUT

In this program the only binary number is allowed as input. There may have different logic for conversion. But the main thing we have to care about is correct result.

Leave a Reply

Your email address will not be published.