C语言查找数字和:找出100到300之间数位和为8的数字
C语言查找数字和:找出100到300之间数位和为8的数字
以下是用C语言编写的代码片段,用于找出100到300之间数位和为8的数字,并计算这些数字的总和:
#include<stdio.h>
int main() {
int number, hundreds, tens, units, sum = 0;
printf('符合条件的数字: ');
for (number = 100; number <= 300; number++) {
hundreds = number / 100;
tens = (number / 10) % 10;
units = number % 10;
if ((hundreds + tens + units) == 8) {
printf('%d ', number);
sum += number;
}
}
printf('
这些数字的总和: %d
', sum);
return 0;
}
运行此代码时,它将输出100到300之间数位和为8的数字,以及这些数字的总和。
希望这可以帮助到你!如果您还有其他问题,请告诉我。
原文地址: https://www.cveoy.top/t/topic/kIn 著作权归作者所有。请勿转载和采集!