C语言怎样提取一个数的十位个位百位千位?

设一个数为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语言的运算符包含的范围很广泛,共有34种运算符。C语言把括号、赋值、强制类型转换等都作为运算符处理。从而使C语言的运算类型极其丰富,表达式类型多样化。灵活使用各种运算符可以实现在其它高级语言中难以实现的运算。

猜你喜欢

转载自www.cnblogs.com/Ph-one/p/12176136.html