This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: else printf("%d is prime\n", n); 3. [35 p ] Write a program that converts a number from decimal to binary. Recall that to convert a decimal number to any base we repeatedly divide the number to the base, replacing the number with the quotient each time, until the quotient is 0. To get the result we read the remainders in reversed order. Hint: use an array to store the remainders. To minimize memory usage you may want to use an array of chars. You may assume that the binary number will not be longer that 100 digits. Answer: #include <stdio.h> #define BASE 2 #define MAX_DIGITS 100 int main() { char bin[MAX_DIGITS]; int i=0, k, n; printf("Enter decimal number "); scanf("%d", &n); while (n > 0) { bin[i] = n % BASE; n = n / BASE; i++; } printf("Number in binary: "); for (k = i  1; k >= 0; k) printf("%d", (int)bin[k]); printf("\n"); return 0; } 2...
View
Full Document
 Summer '08
 Staff
 Numerical digit, Binary numeral system, Decimal, Numeral system, #define

Click to edit the document details