怎么保护钟无艳和墨子:用C语言编输入一个整数输出其位数

来源:百度文库 编辑:高考问答 时间:2024/04/29 14:01:24
急啊,请各位帮忙啊
还是不行啊。我快急死了啊。请大家救救我吧

main(){
int i;
scanf("%d",&i);
int k=0;
while(i){
k++;
i=i/10;
}
printf("%d",i);
}

获取输入数据比较简单,用scanf即可。

下面给两个判断整型数据位数的函数:
1. 直接求int类型数据位数:
int GetLength(const int tmp)
{
int count=0;
while( tmp/10 )
count++;
return count;
}

2. 利用字符数组来变通的获取:
int GetLength(const int tmp)
{
char str[16];
memset(str, 0, sizeof(str));
sprintf(str, "%d", tmp);

return strlen(str);
}

/*
版权所有 陈冠钢
用C语言编输入一个整数输出其位数
*/

#include<stdio.h>

void main()
{
int number,sum=0;
printf("enter number:\n");
scanf("%d",&number);

while(number>0)
{
number/=10;
sum++;
}
printf("\n%d",sum);
}

对10取对数 加1

#include <ctype.h>
#include <stdio.h>
main(){
int i,k=0;
scanf("%d",&i);
if(isdigit(i)){
while(i){
k++;
i=i/10;
}
}else
printf("plese input an integer:");
printf("%d\n",k);
getch();
}

小心溢出

#include<stdio.h>
main(){
int i;
scanf("%d",&i);
int k=0;
while(i){
k++;
i=i/10;
}
printf("%d\n",k);

}
这个是好的 ~~