c语言一个小程序,大家帮忙看下!!

2024-10-31 04:20:18
有5个网友回答
网友(1):

改的:(把n<=100加在while()中,同时成立就可以了)
#include
void main()
{ int s1,s2,n;
s1=0;s2=0;

while(n%2==0&&<=100)
{ s2=s2+n;
n++;}
while(n%2!=0&&<=100)
{s1=s1+n;
n++;}

printf("s2=%d\n",s2);
printf("s1=%d\n",s1);
}

我写的:
#include
void main()
{
int n,sum1=0,sum2=0;
for(n=0;n<=100;n++)
{
if(n%2==0)
{sum1+=n;}
else
{sum2+=n;}
}
printf("sum1=%d,sum2=%d",sum1,sum2);
}
在楼上的基础上修改,但是思想不一样!

网友(2):

在判断是奇或偶数的时候
应该用条件判断语句if
而但你用两个循环.则只执行一次循环
在你的基础上改下

#include
void main()
{ int s1,s2,n,i=0;
s1=0;s2=0;

while(i<=100)//用循环控制次数
{
if(n%2==0)
{ s2=s2+n;
n++;}
else if(n%2!=0)
{s1=s1+n;
n++;}
else
{
printf("错误\n");
return;
}
}
printf("s2=%d\n",s2);
printf("s1=%d\n",s1);
}

网友(3):

已改好

#include
void main()
{ int s1,s2,n=1;
s1=0;s2=0;

while(n<=100)
{if(n%2==0)
s2=s2+n;
n++;}
n=1;
while(n<=100)
{if(n%2!=0)
s1=s1+n;
n++;}

printf("s2=%d\n",s2);
printf("s1=%d\n",s1);
}

我写的,交流一下:
#include
void main()
{
int n,sum1=0,sum2=0;
for(n=1;n<100;n+=2)
sum1+=n;
for(n=2;n<=100;n+=2)
sum2+=n;
printf("sum1=%d,sum2=%d",sum1,sum2);
}

网友(4):

你的循环是一个死循环,程序是执行不完的.
这是最简单的改法
#include
void main()
{ int s1,s2,n=2
s1=1;s2=0;
while(n%2==0&&n<=100)
{ s2=s2+n;
n++;}
while(n%2!=0&&n<=100)
{s1=s1+n;
n++;}
printf("s2=%d\n",s2);
printf("s1=%d\n",s1);
}

网友(5):

#include
void main()
{ int s1,s2,n;
s1=0;s2=0;
n=0;

while(n%2==0&&n<=100)
{ s2=s2+n;

n++;}
n=0;
while(n%2!=0&&n<=100)
{s1=s1+n;
n++;}

printf("s2=%d\n",s2);
printf("s1=%d\n",s1);
}