1
0

binary.c 614 B

12345678910111213141516171819202122232425262728293031
  1. /* binary.c -- prints integer in binary form */
  2. #include <stdio.h>
  3. void to_binary(unsigned long n);
  4. int main(void)
  5. {
  6. unsigned long number;
  7. printf("Enter an integer (q to quit):\n");
  8. while (scanf("%lu", &number) == 1)
  9. {
  10. printf("Binary equivalent: ");
  11. to_binary(number);
  12. putchar('\n');
  13. printf("Enter an integer (q to quit):\n");
  14. }
  15. printf("Done.\n");
  16. return 0;
  17. }
  18. void to_binary(unsigned long n) /* recursive function */
  19. {
  20. int r;
  21. r = n % 2;
  22. if (n >= 2)
  23. to_binary(n / 2);
  24. putchar(r == 0 ? '0' : '1');
  25. return;
  26. }