Header Ads Widget

Write a program to convert binary number in to decimal number.

 Code:-

#include<stdio.h>
#include"string.h"
#include<math.h>
int main()
{
int n=0,k=0,i,lenght,d;
char bin[100],x;
printf("Enter a binary number\n");
scanf("%s",bin);
lenght=strlen(bin);
for(i=lenght-1;i>=0;i--)
{
if(bin[i]=='1')
{
n=n+pow(2,k);
k++;
}
else
k++;
}
printf("Decimal of the binary is: %d",n);
return 0;
}

Output:-

Enter a binary number
101
Decimal of the binary is: 5