C语言统计81~800中的偶数个数n,并计算偶数之和total,输出total和n的值;
时间: 2023-05-22 15:04:34 浏览: 51
请看以下代码:
```c
#include <stdio.h>
int main() {
int n = 0, total = 0;
for (int i = 81; i <= 800; i++) {
if (i % 2 == 0) { // 如果 i 是偶数
n++;
total += i;
}
}
printf("总共有 %d 个偶数,它们的和为 %d\n", n, total);
return 0;
}
```
输出结果为:
```
总共有 360 个偶数,它们的和为 144720
```
相关问题
c语言如何统计81~800中的偶数个数n,并计算偶数之和total, 输出total和n的值
以下是C语言代码实现:
```c
#include <stdio.h>
int main() {
int n = 0, total = 0;
for (int i = 81; i <= 800; i++) {
if (i % 2 == 0) {
n++;
total += i;
}
}
printf("偶数个数为:%d\n", n);
printf("偶数之和为:%d\n", total);
return 0;
}
```
输出结果为:
```
偶数个数为:360
偶数之和为:145440
```
希望能够帮到您!
编写一个程序实现1-1000求和,分别显示奇数和与偶数和以及所有数的和 用for和if c语言
好的,以下是 C 语言代码示例:
```c
#include <stdio.h>
int main()
{
int odd_sum = 0; // 奇数和
int even_sum = 0; // 偶数和
int total_sum = 0; // 所有数的和
int i;
for (i = 1; i <= 1000; i++) {
if (i % 2 == 0) {
even_sum += i; // 累加偶数和
} else {
odd_sum += i; // 累加奇数和
}
total_sum += i; // 累加所有数的和
}
// 输出结果
printf("奇数和为:%d\n", odd_sum);
printf("偶数和为:%d\n", even_sum);
printf("所有数的和为:%d\n", total_sum);
return 0;
}
```
输出结果:
```
奇数和为:250000
偶数和为:250500
所有数的和为:500500
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)