Write a program to print the sum of the following series upto 'n' term. s=2-4+6-8+10-12…......n
Answers
Answered by
1
s=( x:x is even number, where2x-4x+6x-8x....n)
Khushi2111:
But this is a computer question...
Answered by
2
Question:-
Write a program to find the sum of the following series.
S=2-4+6-8+10-12+14-.....N terms.
Program:-
import java.util.*;
class Sum
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of terms for the series sum: ");
int n=sc.nextInt();
int b=2, i=1, s=0;
for(i=1;i<=n;i++, b+=2)
{
if(i%2==1)
s+=b;
else
s-=b;
}
System.out.println("Sum of the series is: "+s);
}
}
Similar questions