C Program to convert number binary to decimal

C Program to convert number from binary to decimal: Binary numbers contain only 0 and 1 and decimal numbers contain 0 to 9 digits. Numbers in binary system are 100, 1010, 11001. Numbers in decimal system are 254, 582.
 
/* C Program to convert number binary to decimal */
#include<stdio.h>
#include<math.h>
int main()
{
 int bin,dec=0,i=0;
 printf("Enter a binary number: ");
 scanf("%d",&bin);
 while(bin!=0)
 {
  int rem=bin%10;
  dec=dec+rem*pow(2,i);
  bin=bin/10;
  i++;
 }
 printf("Decimal number = %d",dec);
 return 0;
}
/* End of the program */
 

Another C program to convert number from binary to decimal suing string
 
/* C Program to convert number binary to decimal */
#include<stdio.h>
#include<math.h>
#include<string.h>
int main()
{
 char bin[32];
 int i=0,j=0,n,dec=0;
 printf("Enter a binary number: ");
 scanf("%s",bin);
 n=strlen(bin);
 for(i=n-1;i>=0;i--)
 {
  int temp=bin[i]-'0';
  dec=dec+temp*pow(2,j);
  j++;
   }
 printf("Decimal number = %d",dec);
 return 0;
}
/* End of the program */
 



Popular posts from this blog