题目
编写一程序实现输出100以内的偶数和,求2+4+6+……+100的和
程序
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a, b;
b = 0;
for (a = 1; a <= 100; a++)
{
if (a % 2 == 0)
b = b + a;
}
printf("100以内的偶数和为%d\n",b);
system("pause");
}
思路
使用for语句在100以内执行反复+1的操作,并使用if语句过滤出偶数,输入累加器b中求和,输出结果
结果