Thread: binary to decimal

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    6

    binary to decimal

    I need to convert a binary number to a decimal number

    does this look good:

    Code:
    #include <stdio.h>
    
    int main()
    {
    
    int digit, number = 0, binary, counter1 = 1, counter2, div = 1, mod = 10, bi = 1;
    
    	printf( "Enter a number in binary(12 max): " );
    	scanf( "%d", &binary );
    
    	while ( counter1 <=12 ) {
    
    		 counter2 = 1;
    
    		while ( counter2 == 1 ) {
    		     digit = ( binary % mod ) / div;
    		     mod *= 10;
    		     div*= 10;
                         counter2++;
    		}
    
    		number = number + digit * bi;
    		bi *= 2;
    		counter1++;
    	}
    
    	printf( "The decimal equivalent is: %d\n", number );
    
    	return 0;
    }
    Last edited by juancardenas; 02-19-2003 at 10:50 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 03:52 AM
  2. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 07:52 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. binary to decimal
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 03-14-2004, 08:35 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM