一个小问题拜托大家!谢谢!!
求2个数之间所有奇数的和main()
{
int a,b,i,s,sum;
scanf("%d %d",&a,&b);
if(a>b)
s=a;a=b;b=s;
if((a+1)%2==0)
a+=1;
for(i=a+1;i<b;i+=2)
sum=i;
sum=sum+i;
printf("%d",sum);
getch();
}
请问哪里错了 谢谢
#include <stdio.h>
void main() {int a,b,i,s,sum=0; printf("the first one is:\n"); scanf("%d",&a); printf("the 2nd is:\n"); scanf("%d",&b); if(a>b) {s=a;a=b;b=s;} if((a+1)%2==0) {for(i=a+2;i<b;i+=2) sum=sum+i;} if((a)%2==0) {for(i=a+1;i<b;i+=2) sum=sum+i; } printf("sum=%d",sum); }
其实就是2种情况:判断小数是偶数还是奇数就可以了。