C语言 编写一个程序,输入一个正整数,求出它是几位数。 C语言: 输入一个正整数,求出它是几位数;输出原数和位数

C\u8bed\u8a00 \u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570\uff0c\u6c42\u51fa\u5b83\u662f\u51e0\u4f4d\u6570\u3002

\u601d\u8def\uff1a\u6c42\u4e00\u4e2a\u6b63\u6574\u6570n\u7684\u4f4d\u6570\u53ef\u4ee5\u5148\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cfnum\uff0c\u5e76\u521d\u59cb\u5316\u4e3a0\uff0c\u4f9d\u6b21\u628a\u8be5\u6574\u6570n\u9664\u4ee510\uff0c\u76f4\u5230\u5176\u4e3a0\u4e3a\u6b62\uff0c\u5e76\u4e14\u6bcf\u9664\u4e00\u6b2110\uff0c\u53d8\u91cfnum\u7684\u4e2a\u6570\u5c31\u81ea\u52a01\uff0c\u6700\u540enum\u7684\u503c\u5c31\u662f\u8be5\u6574\u6570n\u7684\u4f4d\u6570\u3002
\u53c2\u8003\u4ee3\u7801\uff1a
#include int main(){ int n,num=0;scanf("%d",&n);while(n){num++;n/=10;}printf("%d\n",num); return 0;}/*\u8f93\u51fa\uff1a1234566*/

#include
#include
int main()
{int i=1;
long int num,num1;
scanf("%ld",&num);
num1=num;
while(num/10){
i++;
num=num/10;
}
printf("number:%ld, width: %d\n",num1,i);
system("pause");
return 0;
}

/*功能:输入一个任意长度的正整数,求出它是几位数。*/
#include<stdio.h>
int main()
{
int number; int count = 0;//记录个数
scanf("%d", &number);
if (number == 0)
{

printf("number is %d\n",count+1);//0是一位数
} else
{

while (number != 0)
{
number = number / 10;
count++;
} printf("number is %d", count);
} return 0;
}

完整代码如下,仅供参考:

#include<stdio.h>
int main()
{
int n,count=0;
scanf("%d", &n);
if (n <= 0)
{
printf("必须输入正整数");
return 1;
} else
{
while (n != 0)
{
n = n / 10;
count++;
}
printf("n是%d位数", count);
}

return 0;
}

扩展阅读:c++编程软件 ... cnc编程必背50个程序 ... c++编译器下载 ... c十十入门编程 ... c语言自动生成器 ... c+语言编程入门自学 ... 初学编程必背50个 ... c十十编程要学多久 ... c语言编译器苹果版 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网