Thread: Decimal to Binary Conversion program

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    21

    Decimal to Binary Conversion program

    Hello, I wrote this program for my Intro to Digital Systems class to convert a base 10 number into a Binary base 2 number string. It works perfectly except for some numbers it slams a number in front of it. On linux this number is -1073742772. Such a number is 60.
    Code:
    #include <stdio.h>
    
    
    int main()
    {
    	int input=0;
    	int count=0;
    	int binary_backwards[32];
    	int binary_string[32];
    	int i=0;
    	int j=0;
    	printf("Enter a base 10 positive number to convert to binary:");
    	scanf("%i", &input);
    
    	while(input >0 && count<32)
    	{	
    		binary_backwards[count] = input %2;
    		input /=2;
    		count++;
    	}
    
    	while(count >-1)
    	{
    		binary_string[i]=binary_backwards[count];
    		count--;
    		i++;
    	}
    	
    	while(j<i)
    	{
    		printf("%i", binary_string[j]);
    		j++;
    	}
    	return 0;
    }
    Anyone have any ideas why this is happening?
    Last edited by acidbeat311; 01-12-2006 at 05:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. decimal to binary
    By kurz7 in forum C Programming
    Replies: 8
    Last Post: 07-10-2003, 12:03 AM
  4. decimal to binary conversion
    By noob2c in forum C Programming
    Replies: 4
    Last Post: 05-29-2003, 08:07 PM
  5. Decimal Points and Binary Points
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 11-07-2002, 01:06 AM