
Please help me to write code for this series Write a Java Method to solve the following problem: a.Evaluate the result of the series: 1-2+3-4+5-6+��+n

hi friend,
Try the following code may this will be helpful for you
package net.roseindia;
import java.util.Scanner;
public class SumOfSeries {
public static void sum(int n)
{
int total = 0;
for(int i=1; i<=n; i++)
{
if(i<n)
{
System.out.print(i+"+");
}
total = total+i;
}
System.out.println(n+" = "+total);
}
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter the last number : ");
int num = scan.nextInt();
sum(num);
}
}

o/p is displaying 1+2+3+4+5=15
But i need o/p as 1+2-3+4-5=-1