码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-01-10 15:41:36      阅读:3174      评论:0      收藏:0      [点我收藏+]

标签:运行   类型   oop   get   title   color   lan   class   类型转换   

设一个数为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语言的运算类型极其丰富,表达式类型多样化。灵活使用各种运算符可以实现在其它高级语言中难以实现的运算。

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

标签:运行   类型   oop   get   title   color   lan   class   类型转换   

原文地址:https://www.cnblogs.com/Ph-one/p/12176136.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!