设一个数为n,则在C语言中其个位、十位、百位、千位依次这样计算zhi: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语言怎样提取一个数的十位个位百位千位?
整除:10/8=1
取模:10%8=2
对取值各个位上的单数操作
// 取数值各个位上的单数之和
int getSum(int n) {
int sum = 0;
while (n) {
sum += (n % 10) * (n % 10);
n /= 10;
}
return sum;
参考链接(代码随想录公众号):哈希表:今天你快乐了么?