C Language Examples - Determining the Number of Digits
The user enters a number, and the program determines how many digits it has.
Example
#include
<
stdio.h
int
main
(
)
{
long
long
n;
int
count
=
0
;
printf
(
"
Enter an integer:
"
)
;
scanf
(
"
%lld
"
, &
n
)
;
while
(
n
!=
0
)
{
// n = n/10
n
/=
10
; ++
count
;
}
printf
(
"
The number has %d digits.
"
,
count
)
;
}
Running result:
输入一个整数: 2345
数字是 4 位数。
C Language Examples