write a program in Java to find the sum of series of s= 2-4+6-8....to n terms
Answers
Answered by
4
Explanation:
We loop all even number upto n, and then subtract multiples of 4 and add the rest.
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);
Attachments:
Similar questions