print the series 2-4+6-8+10.....n using java

 int a=2,n=10;  // n for number of terms

//a is the starting value

int sum=0;

for(int i=1;i<=n;i++)

{

if(i%2==0)

{

sum=sum-a;

}

else

{

sum=sum+a;

}

a=a+2;

}

System.out.println(sum);

Comments

Popular posts from this blog