Thread: Digital to Binary

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    Digital to Binary

    I'm trying to figure out how to add the 0's in front of the binary out put.

    ie: when I enter 23 I get 10111 I would like it to read 00010111 or 0001 0111

    Thanks for any help you can give me on this.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void)
    {
        long b[20], n, r, c = 0, i ;
        printf("Enter a decimal number: ");
        scanf("%ld", &n) ;
    
        if(n <= 0 || n > 255)
        {
            printf("Only numbers between 1 - 255");
            return 1;
        }
        while(n > 0)
        {
            r = n % 2 ;
            b[c] = r ;
            n = n / 2 ;
            c++ ;
        }
        printf("The binary equivalent is : ");
    
        for(i = c - 1 ; i >= 0; i--)
        {
            printf("%ld", b[i]) ;
        }
    }
    Last edited by brosskgm; 11-09-2012 at 09:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. digital watermark
    By not_so_cool in forum C Programming
    Replies: 1
    Last Post: 03-07-2011, 11:16 PM
  2. digital simulator
    By fuzon87 in forum C Programming
    Replies: 1
    Last Post: 03-29-2008, 01:06 PM
  3. Digital Logic
    By strokebow in forum Tech Board
    Replies: 3
    Last Post: 12-09-2006, 01:05 PM
  4. Digital art
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 07-31-2003, 05:32 AM
  5. Digital mixing in DOS
    By VirtualAce in forum Game Programming
    Replies: 0
    Last Post: 01-11-2002, 08:05 AM