c语言编程。初学者的问题

问题。输入一个正整数你,计算1-3+5-7。。。。。。。。。的前n项之和,
我的程序
#include <stdio.h>
#include <math.h>
int main(void)
{
int n,x=0,y,i=0;
printf("Enter n:");
scanf("%d",&n);
for(x<=n;i=i+1;)
if(i%2!=0)
x=x+1+2*i;
y=pow(-1,i)*x+1;
else

x=1+2*i;
y=x+1;
printf("%d\n",y);
return 0;
}

提示的问题是D:\123\123.cpp(42) : warning C4552: '<=' : operator has no effect; expected operator with side-effect
D:\123\123.cpp(46) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
D:\123\123.cpp(48) : error C2181: illegal else without matching if
Error executing cl.exe.
123.obj - 1 error(s), 2 warning(s)

怎么修改???

第1个回答  推荐于2017-09-14
for(x<=n;i=i+1;)这一句有问题,应该是for(x=n;i=i+1;),for循环第一个表达式不是逻辑判断,而是赋循环变量初始值的本回答被提问者采纳
第2个回答  2015-10-23
#include <stdio.h>
#include <math.h>

int main(void)
{
int s=0,n,i;
printf("Enter n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2!=0)
s+=(2*i-1);
else
s-=(2*i-1);
}
printf("%d\n",s);
return 0;
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网