1.
输入待计算的数x
2.
定义一个数位的变量int
n=0
3.
循环,x每次缩小10倍,n=n+1
最终x会变成0,循环结束
4.
输出n值
代码:
#include
void main()
{
int x,m,n=0
printf("input x: ")scanf("%d", &x )
m=x//备份原数,供输出用
do {
n++
x /= 10
} while( x>0 )
printf("%d 是%d位数\n", m, n )
}
如果n是个多位十进制数,那么n%10是个位数字,n/10%10是十位数字,n/100%10是百位数字,依此类推。
设一个数为n,则在C语言中其个位、十位、百位、千位依次这样计算:n/1%10,n/10%10,n/100%10,n/1000%10
代码如下:
#include<stdio.h>
int main(){
int n = 123456
int unitPlace = n / 1 % 10
int tenPlace = n / 10 % 10
int hundredPlace = n / 100 % 10
int thousandPlace = n / 1000 % 10
printf("个位:%d\n十位:%d\n百位:%d\n千位:%d\n", unitPlace, tenPlace, hundredPlace, thousandPlace)
getchar()
return 0
}
扩展资料:
C语言包含的各种控制语句仅有9种,关键字也只有32 个,程序的编写要求不严格且以小写字母为主,对许多不必要的部分进行了精简。实际上,语句构成与硬件有关联的较少,且C语言本身不提供与硬件相关的输入输出、文件管理等功能,如需此类功能,需要通过配合编译系统所支持的各类库进行编程,故c语言拥有非常简洁的编译系统。
参考资料来源:百度百科-c语言