Solution of URI 1159::Sum of Consecutive Even 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 N,i=0,sum;
while(1) {
sum=0;
scanf("%d",&N);
if(N==0) {
break;
}
if(N%2==0) {
for(i=N;i<=N+8;i=i+2) {
sum += i;
}
}
else {
for(i=N+1;i<=N+9;i=i+2) {
sum += i;
}
}
printf("%d\n",sum);
}
return 0;
}
No comments