C语言中关于求一个数的各个位数上面的数的求法

求一个数N的个位十位百位千位上的数字:
在C语言中其
个位:n/1%10
十位:n/10%10
百位:n/100%10
千位:n/1000%10
全部都是最后对10求余,也就是说最后的数是从0至9的数,然后前面的整除的话,就看是哪个位上面的数就除以相对应的位数。

#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;
}

猜你喜欢

转载自blog.csdn.net/sun_fengjiao/article/details/86492354