求奇数列或偶数列的前n项和
我用安阳工学院的Turbo c/c++ 2.0编个求奇数列或偶数列的前n项和(如下),通过了编译,可就是结果就是不正确,请高手指点迷津!
/* Note:Your choice is C IDE */
#include "stdio.h"
main()
{
int i,sum=0,n;
printf("please input the first (n1) number: ");
scanf("%d",&i);
printf("please input n:");
scanf("%d",&n);
while(i%2==1 && i<=n)
{
sum=sum+i;
i=i+2;
}
while(i%2==0 && i<=n)
{
sum=sum+i;
i=i+2;
}
printf("sum=%d",sum);
getch();
}