只需要设计一个计时器,因为C语言中除法只留下整数部分,所以可以拿数字/10;数字位数即为循环次数,待n为个位时n/10=0,然后循环结束。
- #include<stdio.h>
- int main()
- {
- int n;
- int count = 0;
-
- scanf("%d",&n);
- if(n == 0)//n为0的情况,个人感觉无位数,退出
- {
- return 0;
- }
- else
- {
- while(n)
- {
- n=n/10;//每次去掉数字最后一位
- count++;//循环一次计数器+1
- }
- }
- printf("%d",count);
-
- return 0;
- }
-
-