fork download
  1. #include <stdio.h>
  2.  
  3. void printNum(int num) {
  4.  
  5. if(num == 0) {
  6. printf("0\n"); // Check for num being 0.
  7. return;
  8. }
  9.  
  10. num = num < 0 ? num*-1 : num; // Make sure the number has no sign bit.
  11. char first1Found = 0; // Create a check for the first 1 printed.
  12. for (int i = sizeof(num)*8 - 1; i >= 0 ; --i) {
  13. if (num & (1 << i)) { // If its a 1, print it and set the first1Found bool.
  14. printf("1");
  15. first1Found = 1;
  16. } else if(first1Found) { // If its a 0 only print it if its not a leading 0.
  17. printf("0");
  18. }
  19. }
  20. printf("\n");
  21.  
  22.  
  23. }
  24.  
  25.  
  26.  
  27. int main(void) {
  28.  
  29. printNum(0);
  30. printNum(10);
  31. printNum(20);
  32. printNum(1234);
  33. printNum(117653);
  34.  
  35. // your code goes here
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0s 2112KB
stdin
Standard input is empty
stdout
0
1010
10100
10011010010
11100101110010101