Given a number of terms the task is to calculate the value upto ‘n’ terms of the below given series.
Series: 1 – 2 + 3 – 4 + 5 …………… + ‘n’ terms.
Java
import java.util.*;
public class SeriesV5
{
public static void main(String args[])
{
int n=0;
int result=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter number of terms: ");
n=sc.nextInt();
for(int i=1;i<=n;i++)
{
if(i%2==0)
{
result=result-i;
}
else
{
result=result+i;
}
}
System.out.println("Result of the series: "+result);
}
}Java