Solution of URI 1158::Sum of Consecutive Odd Numbers III
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 N,X,Y,sum=0,i;
scanf("%d",&N);
while(N--){
scanf("%d%d",&X,&Y);
sum=0;
if(X%2==0){
X +=1;
while(Y--){
sum += X;
X += 2;
}
}
else{
while(Y--){
sum += X;
X += 2;
}
}
printf("%d\n",sum);
}
return 0;
}
No comments