Thread: decimal to binary

  1. #1
    jk81
    Guest

    decimal to binary

    this program suppose to change decimal number to binary..
    but..
    when i run this program, somehow this does not terminate the loop.
    I put break; command to terminate the loop.. but, it doesn't work i think..
    but after manually terminate the loop, it displays right answer though..

    Code:
    #include <stdio.h>
    
    main() 
    { 
    char bData[100]; 
    int count=0;
    int inData;
    int i;
    int n = 1;
    int tmp; 
    
    printf("Enter a number: ");
    scanf("%d\n", &inData); 
    tmp = inData; 
    count = 0; 
    
    for(;;)  
    { 
    bData[count] = tmp%2; 
    
    if(tmp < 2) 
    break; 
    
    tmp /= 2; 
    count++; 
    }
    
    printf("Binary number for %d is ",inData); 
    for(i=count; i>=0; i--) 
    {
    printf("%1d",bData[i]); 
    }
    }

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

    decimal to binary

    oops i used code tag wrong...

    this program suppose to change decimal number to binary..
    but..
    when i run this program, somehow this does not terminate the loop.
    I put break; command to terminate the loop.. but, it doesn't work i think..
    but after manually terminate the loop, it displays right answer though..

    Code:
    #include <stdio.h>
    
    main() 
    { 
    char bData[100]; 
    int count=0;
    int inData;
    int i;
    int n = 1;
    int tmp; 
    
    /*Get input */
    printf("Enter a number: ");
    scanf("%d\n", &inData); 
    tmp = inData; 
    count = 0; 
    
    for( ; ; )  /*infinite loop. stop when the number is less than 2 */
    { 
    bData[count] = tmp%2; 
    
    if(tmp < 2) 
    break; 
    
    tmp /= 2; 
    count++; 
    }
    
    printf("Binary number for %d is ",inData); 
    for(i=count; i>=0; i--) 
    {
    printf("%1d",bData[i]); 
    }
    }

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    The line:
    scanf("%d\n", &inData);

    should be
    scanf("%d", &inData);
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    32
    Thank you~!!!
    it Works!!!

  5. #5
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    there's an easier way to do it:
    Code:
    int count, value,temp;
    value = 56; //any number would do. this program only reads the first 8 bits
    //however, you can make count=31 to make it 32 bits. 
    temp = value;
    for (count=7;count >= 0;count--) 
        printf("%c",temp & (2 << count) ? '1' : '0');
    //edit: changed 7 to count... thanks salem for alerting me to things like that

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