/* Convert positive number to binary representation. */ #include #define LENGTH 1000 /* Maximal length of binary number */ /* Convert positive number to binary representation Input: reads number from standard input Output: prints binary representation to standard output */ int main() { int n, k, i; int B[LENGTH]; /* Read number from standard input, store in n */ if (scanf("%d", &n) == 1) { k = 0; while (n > 0 && k < LENGTH) { B[k] = n % 2; n = n / 2; k = k + 1; } if (k < LENGTH) { printf("Binary representation: "); for (i = k-1; i >= 0; i--) printf("%d", B[i]); printf("\n"); return 0; } else { printf("Binary represenation longer than %d bits.\n", LENGTH); return -1; } } else { printf("No valid number read.\n"); return -1; } }