#include "stdio.h"
int main(int argc,char *argv[]){
int sum,i;
for(sum=0,i=1;i<=99;sum+=i,i+=2);//for求和
printf("Use 'for' summing:\n1+3+5+...+99 = %d\n",sum);
i=sum=1;
while(sum+=i+=2,i<99);//while求和
printf("Use 'while' summing:\n1+3+5+...+99 = %d\n",sum);
i=sum=1;
do//do~while求和
sum+=i+=2;
while(i<99);
printf("Use 'do~while' summing:\n1+3+5+...+99 = %d\n",sum);
return 0;
}
#include
int main()
{
int i,sum=0;
for(i=0;i<100;i++)
{
if(i%2!=0)
{
sum=sum+i;
}
}
printf("%d\n",sum);
return 0;
}
#include
int main()
{
int i=0,sum=0;
while(i<100)
{
if(i%2!=0)
{
sum=sum+i;
}
i++;
}
printf("%d\n",sum);
return 0;
}
#include
int main()
{
int i=0,sum=0;
do
{
if(i%2!=0)
{
sum=sum+i;
}
i++;
}while(i<100);
printf("%d\n",sum);
return 0;
}
int s=0 和为S
for(int i=0;i<100;i++){if(i%2!=0){s=s+i;}}
int i=0
while(true){
i=i+1;
if(i<100){
if(i%2!=0){s=s+i;}
}else{break;}
}
int i=0;
while(true){
i=i+1;
if(i%2!=0){s=s+i;}
else{break;}
}do(i<100)
int s=0 和为S
for(int i=0;i<100;i++){if(i%2!=0){s=s+i;}}
int i=0