给出一个不多于五位的正整数,1.求出它是几位数2.分别输出每一位数字
一般方法
#include<stdio.h>
int main()
{
int n, m, i = 1, x;
printf("输入n:");
scanf("%d", &n);
m = n;
x = n;
if (n>99999 || n <= 0)
{
printf("请输入一个不多于5位数的正整数\n");
}
while (n >= 10)
{
i++;
n = n / 10;
}
printf("这是一个%d位数\n", i);
还可以使用丢弃个位的方法
这里数字用来测试算法
int Count(int n)
{
if(n == 0)
return 1;
int tmp = 0;//计数器
while(n != 0)
{
tmp++;
n /= 10;//丢弃个位
}
return tmp;
}
int main()
{
printf("%d\n",Count(45679));
}