求助
计算1到100之间的奇数之和及偶数之和,并输出。请在程序中的横线上填入适当的内容,将程序补充完整并运行。#include <stdio.h>
void main( )
{ int asum,b,csum,i;
_____________;
_____________; /*变量初始化*/
for(i=0; i<=100; i+=2)
{ asum+=i; /*变量asum存放偶数的和*/
____________;
____________
csum+=b; /*变量csum存放奇数的和*/
}
printf("sum of evens is %d\n",asum);
printf("sum of odds is %d\n",________);
}
#include <stdio.h>
void main( )
{
int asum=0, b=1, csum=0, i;
for(i=1; i<=100; i+=2)
{
asum+=i; /* 变量asum存放奇数的和 */
b+=2;
csum+=b; /* 变量csum存放偶数的和 */
}
printf("sum of evens is %d\n",asum);
printf("sum of odds is %d\n",csum);
}
球一个最佳答案谢谢啦!这对我非常重要!{:10_254:}{:10_254:}{:10_254:} #include <stdio.h>
void main( )
{
int asum = 0, b = 0, csum = 0, i;
for(i=1; i<=100; i+=2)
{
csum+=i; /*变量csum存放奇数的和*/
}
for(i=0; i<=100; i+=2)
{
asum+=i; /*变量asum存放偶数的和*/
}
printf("sum of evens is %d\n", asum);
printf("sum of odds is %d\n", csum);
} #include <stdio.h>
void main( )
{
int asum = 0, b, csum = 0, i; // 变量初始化
for(i=1; i<=100; i+=2) // 循环从1开始,步长为2,即遍历奇数
{
csum += i; // 变量csum存放奇数的和
}
for(i=0; i<=100; i+=2) // 循环从0开始,步长为2,即遍历偶数
{
asum += i; // 变量asum存放偶数的和
}
printf("sum of evens is %d\n", asum);
printf("sum of odds is %d\n", csum);
}
运行这段代码将会得到输出结果:
sum of evens is 2550
sum of odds is 2500
页:
[1]