Computer Science, asked by Xavier8405, 9 months ago

2+4+6+8... nterms find java program

Answers

Answered by vigneshsatheesh
1

Answer:

public class SumOfSeries{

public static void main(String args[]){

int n = 20;int sum =0;

for(int i=2;i<=n;i=i+2){

if(i%4==0){ sum=sum-i;}

else{ sum=sum+i;}

}

System.out.println("Sum of series upto " + n + " terms is : " + sum);

}

}

Explanation:

We loop all even number upto n, and then subtract multiples of 4 and add the rest

So everytime the number (i) is a multiple of four, the sum will be decremented by it and otherwise i is added to the sum.

Similar questions