使用while:
#include <stdio.h>
int main(){
int sum=0;
int num=1;
int sum2=0;
int num2=2;
while(num<100){
sum=sum+num;
num=num+2;
}
printf("奇数和为:%d\n",sum);
while(num2<=100){
sum2=sum2+num2;
num2=num2+2;
}
printf("偶数和为:%d\n",sum2);
}
使用for:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num=0,a=0,b=0;
for(;num<=100;num++)
{
if(num%2==0)
a+=num;
else
b+=num;
}
printf("一百以内偶数和是%d\n",a);
printf("一百以内齐数和是%d\n",b);
return 0;
}
或
#include <stdio.h>
int main(){
int sum1=0;
int sum2=0;
int num1,num2;
for(num1=1;num1<100;num=num1+2){
sum1=sum1+num1;
}
printf("奇数和%d\n",sum1);
for(num2=2;num2<=100;num2=num2+2){
sum2=sum2+num2;
}
printf("偶数和%d\n",sum2);
}