Solution of URI 1071::Sum of Consecutive Odd Numbers
Before seeing the solution make sure that you have tried enough. Don’t copy the whole code, just find out the logic. If you feel any problem, you will inform me by comment.
#include <stdio.h>
int main()
{
int n1,n2,big=0,small=0,i;
long long int sum=0;
while(scanf("%d%d",&n1,&n2) !=EOF) {
big=n1>n2? n1:n2;
small=n1<n2? n1:n2;
for(i=small+1;i<big;i++) {
if(i%2 ==-1 || i%2==1) {
sum+=i;
}
}
printf("%lld\n",sum);
sum=0;
}
return 0;
}
No comments