全心思齐网

c语言中十位上的数怎么表示?

如果n是个多位十进制数,那么n%10是个位数字,n/10%10是十位数字,n/100%10是百位数字,依此类推。 设一个数为n,则在C语言中其个位、十位、百位、千位依次这样计算:n/1%10,n/10%10,n/100%10,n/1000%10 代码如下: #include 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; }

匿名回答于2023-10-09 11:00:57


相关知识问答