KAR98S

decimal To Binary.c

Feb 7th, 2021 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.46 KB | None | 0 0
  1. //Print input integer in binary
  2. #include <stdio.h>
  3. #include <math.h>
  4.  
  5. int main() {
  6.     long long n,nearest;
  7.     scanf_s("%lld", &n);
  8.     nearest = pow(2,floor(log2l((long double)n)));
  9.     printf("%lld - %lld\n\n", n, nearest);
  10.     while(nearest){
  11.         if (n - nearest >= 0) {
  12.             printf("1");
  13.             n -= nearest;
  14.         }
  15.         else
  16.             printf("0");
  17.         nearest /= 2;
  18.         printf("(%lld)", n);
  19.     }
  20.     return 0;
  21. }
Add Comment
Please, Sign In to add comment