求 100 以内(包括100)偶数和奇数的和:
#include<stdio.h> int main() { int num,odd=0,even=0; for(num=2;num<=100;num=num+2) { even += num; odd = odd + num - 1; } printf("Odd number sum is %d\n",odd); printf("Even number sum is %d\n",even); return 0; }
执行以上程序输出结果为:
Odd number sum is 2500 Even number sum is 2550