在c语言中用do-while循环计算1+2+.....+100的和?

2024-11-23 03:31:34
推荐回答(2个)
回答1:

#include

#include 

main()

{

int i=1,sum=0,k=0;

do{

sum+=i;

i++;

}while(i<=100);

int j=1;

do{

k+=j;

j+=3;

}while(j<=31);

printf("%d %d",sum,k);

return 0; 

}

回答2:

int main()
{ int i=1,sum=0;
do
{ sum += i++;
//printf("i = %d, sum = %d\n",i,sum);
} while (i<=100);
printf("sum = %d\n",sum);
return 0;

}