Decimal > Binary

This is a discussion on Decimal > Binary within the C Programming forums, part of the General Programming Boards category; i am trying to create a program that will convert binary to decimal, i have got so far: Code: #include ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    1

    Decimal > Binary

    i am trying to create a program that will convert binary to decimal, i have got so far:

    Code:
    #include <stdio.h>
    
    main(void)
    {
    int num1, num2 = 0;
    
    printf("Please enter a value between 0-255: ");
    scanf("%d",&num2);
    
    for
    
    (num1=128;num1>0;num1=num1/2);
    (num1=64;num1>0;num1=num1/2);
    (num1=32;num1>0;num1=num1/2);
    (num1=16;num1>0;num1=num1/2);
    (num1=8;num1>0;num1=num1/2);
    (num1=4;num1>0;num1=num1/2);
    (num1=2;num1>0;num1=num1/2);
    (num1=1;num1>0;num1=num1/2);
     printf("%d",num1/1);
     num1 = num1 &1;
    }
    but i keep getting errors and i dont know why, any help would be appreciated.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, the errors tell you what the error is (they're helpful that way). But the upshot is you only get one set of parentheses for a for loop. And the other helpful bit about for loop is you don't have to work out what's going to happen in each loop -- the computer can do that. You just tell it num1 = num1/2 and it can work out that it will go from 128, to 64, to 32, etc.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Code:
    for
    
    (num1=128;num1>0;num1=num1/2);
    (num1=64;num1>0;num1=num1/2);
    (num1=32;num1>0;num1=num1/2);
    (num1=16;num1>0;num1=num1/2);
    (num1=8;num1>0;num1=num1/2);
    (num1=4;num1>0;num1=num1/2);
    (num1=2;num1>0;num1=num1/2);
    (num1=1;num1>0;num1=num1/2);
     printf("&#37;d",num1/1);
     num1 = num1 &1;
    }
    That translates to:

    Code:
    for(num1 = 128; num1 > 0; num1 /= 2)
    I think that's why.

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, 04:52 AM
  2. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 08: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, 04: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21