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]) ; } }



LinkBack URL
About LinkBacks



