C Language Examples - Determine the Number of Digits in a Number
The user inputs a number, determine how many digits the number 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);
}
Run result:
输入一个整数: 2345 数字是 4 位数。other extensions