Thread: binary to decimal

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    32

    binary to decimal

    it runs but converts wrong..
    it convert 101101 as 1061.. which should be 45.
    can someone fine error?

    Code:
    /********************************************************************************
    * Description of The Program: Program ask user for input(binary number) then
    * change binary number to decimal number
    *************************************************************************************/
    
    /* Header file used*/
    #include <stdio.h>
    
    /* Main Function */
    main()
    {
    		/* Declarations */
    		int binaryinput; /* Input */
    		int i = 2;       /* base */
    		int power = 0;   /* set power to 0 */
    		int sum = 0;     /* sum of temporary result */
    		int temp = 1;    /* Temporary result */
    		int p;		 /* binary number */
    		int storebin;    /* store input */
    
    		/* Get Input From User*/
    		printf("Enter a binary number: ");
    		scanf("%d", &binaryinput);
    		storebin = binaryinput;
    		
    		if(binaryinput == 0) /*If input is 0 then decimal number is 0 */
    			printf("binaryinput = 0");
    		else /* Else convert to decimal */
    		{		
    			while (binaryinput != 0)
    			{
    			    if ((binaryinput%10) == 1) /* Check remainder is 1 */
    				{
    					if(power == 0) /* if power = 0 then result is 1*/
    					temp = 1;
    					else /* else calculate the power */
    					{
    						for(p = 1; p <= power; p++)
    							temp *= i; /* Calculate 2^power */
    					}	
    					sum += temp; /* Add temporary results */
    					power++;      /* find 2^power+1 */
    					binaryinput = binaryinput/10;  /* go to next digit */
    				}
    				else
    				{
    					power++;
    					binaryinput = binaryinput/10; /* go to next digit */
    				}
    			}
    			binaryinput+=sum; /* Calculate sum of temporary result */
    
    			/* Out put */			
    			printf("The decimal number for binary number %d is = %d .\n", storebin, binaryinput );
    		}
    }
    Last edited by jk81; 09-12-2002 at 10:39 PM.

  2. #2
    grprep
    Guest
    while (binaryinput != 0)
    {

    if ((binaryinput%10) == 1)
    {
    ...
    }
    else
    {
    ...
    }

    temp=1;<--Add this

    }

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