Program To Convert Binary to Decimal Using C Language

**NOTE: The following code for “Program To Convert Binary to Decimal Using C Language” has been written and performed on Ubuntu OS, To run the following code in Windows on Turbo C, You need to add #include as one of the header files and getch() at the end of the code before main() closing brace “}”.


void main(){
int i,k=0,a=0,b=0;
char binnum[30];
printf("Enter a binary number: ");

	}else{	b=b+0*pow(2,k); }
printf("\nDecimal Equivalent for %s is %d\n",binnum,b);



Explanation For The Above Program:

  • Input a binary as a string
  • Store the length of the string in two different variables i.e ‘a’ for looping and ‘k’ for power of the base .
  • Run the loop from 0 to length – 1 or (i<a)
  • Read the string characters one by one and check if it is equal to ‘0’ or ‘1’
  • If the Character is equal to 1 then multiply the 1 by 2 to the power ‘k-1’ and store the sum in integer variable ‘b’
  • Print the resultant integer variable.




Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: