fork download
  1. #include <iostream>
  2.  
  3. unsigned count_digits(unsigned value, unsigned base = 10)
  4. {
  5. unsigned n_digits = 0;
  6. do
  7. {
  8. ++n_digits;
  9. value /= base;
  10. } while (value);
  11.  
  12. return n_digits;
  13. }
  14.  
  15. void print_digit_count(unsigned value)
  16. {
  17. std::cout << "In base 10, " << value << " has " << count_digits(value) << " digits.\n";
  18. std::cout << "In base 2, " << value << " has " << count_digits(value, 2) << " digits.\n";
  19. std::cout << "In base 16, " << value << " has " << count_digits(value, 16) << " digits.\n\n";
  20. }
  21.  
  22. int main()
  23. {
  24. print_digit_count(56);
  25. print_digit_count(0);
  26. print_digit_count(1);
  27. print_digit_count(128);
  28. print_digit_count(0xFFFF);
  29. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
In base 10, 56 has 2 digits.
In base 2, 56 has 6 digits.
In base 16, 56 has 2 digits.

In base 10, 0 has 1 digits.
In base 2, 0 has 1 digits.
In base 16, 0 has 1 digits.

In base 10, 1 has 1 digits.
In base 2, 1 has 1 digits.
In base 16, 1 has 1 digits.

In base 10, 128 has 3 digits.
In base 2, 128 has 8 digits.
In base 16, 128 has 2 digits.

In base 10, 65535 has 5 digits.
In base 2, 65535 has 16 digits.
In base 16, 65535 has 4 digits.