fork(1) download
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. int main() {
  5. char valor[1000];
  6. long long int valor_i;
  7. int position = 0, count = 0;
  8. scanf("%lld", &valor_i);
  9. while (valor_i > 0) {
  10. int bit = valor_i % 2;
  11. valor[position++] = '0' + bit;
  12. count += bit;
  13. valor_i /= 2;
  14. }
  15. valor[position] = '\0';
  16. size_t size = strlen(valor);
  17. for (int count = 0; count <= size / 2 - 1; count++) {
  18. char tmp = valor[count];
  19. valor[count] = valor[size - count - 1];
  20. valor[size - count - 1] = tmp;
  21. }
  22. printf("%s - quantidade de '1' => %d\n", valor, count);
  23. return 0;
  24. }
Success #stdin #stdout 0s 2172KB
stdin
14
stdout
1110 - quantidade de '1' => 3