比如一个数9527,需要计算9 + 5 + 2 + 7 = 23
主要是考察如何分解一个四位数,学会分解一个整数之后,然后求和即可。
示例代码如下:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
void main() {
int num = 9527;
int qian, bai, shi, ge, sum;
qian = num / 1000;
bai = num % 1000 / 100;
shi = num % 100 / 10;
ge = num % 10;
printf("qian=%d, bai=%d, shi=%d, ge=%d \n", qian, bai, shi, ge);
sum = qian + bai + shi + ge;
printf("%d的各个位数之和为%d \n", num, sum);
}
运行结果如下:
标签:qian,sum,四位数,位上,案例,ge,num,shi,bai From: https://www.cnblogs.com/yilangcode/p/17731597.html