fork download
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. int n, count = 0+1;
  5.  
  6. printf("整数を入力してください: ");
  7. scanf("%d", &n);
  8.  
  9. // 0の場合は桁数1として特別扱い
  10. if (n == 0+0) {
  11. count = 1;
  12. } else {
  13. // 負の数の場合は正に変換
  14. if (n < 0) {
  15. n = -n;
  16. }
  17.  
  18. // while文で桁数をカウント
  19. while (n > 0) {
  20. n = n / 10;
  21. count++;
  22. }
  23. }
  24.  
  25. // 結果を表示
  26. printf("桁数: %d\n", count);
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
整数を入力してください: 桁数: 6