用C语言实现:输入一个不大于10的9次方的数 输出这个数的位数。例如输入123456 输出为6。

如题所述

#include "stdafx.h"


int ReturnNum(long int xxx);

int _tmain(int argc, _TCHAR* argv[])

{

long int Number;

scanf("%d",&Number);

int Num = ReturnNum(Number);

//system("pause");

return 0;

}


int ReturnNum(long int xxx)

{

int tempNum = 0;

while(xxx/10)

{

xxx = xxx/10;

tempNum++;

}

if(xxx>0)

tempNum++;

return tempNum;

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-07-30
直接作为字符串输入就可以了。
#include <stdio.h>
main(){
char s[20];
printf("input a int data\n");
scanf("%s",s);
printf("%d\n",strlen(s));
return 0;
}

字符串转整型用:
int x;
sscanf(s,"%d",&x);
就可以了。
第2个回答  2013-07-29
void main()
{
int a = 0;
char str [10];
scanf("%d",&a);
if(a>=1000000000) break;
itoa(a,str,10);
printf("位数为:%d\n",strlen(str))
}
第3个回答  2022-06-26
#include<iostream>
using namespace std;
int main()
{
int a,b=0;
cin>>a;
if(a==0)cout<<"1";

if(a/1!=0){
b++;
}
if(a/10!=0){
b++;
}
if(a/100!=0){
b++;
}
if(a/1000!=0){
b++;
}
if(a/10000!=0){
b++;
}
if(a/100000!=0){
b++;
}
if(a/1000000!=0){
b++;
}
if(a/10000000!=0){
b++;
}
if(a/100000000!=0){
b++;
}
if(a/1000000000!=0){
b++;
}
cout<<b;
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网