Write a programt print the sum of the series s=1-2+3-4+5+6+7...n
Answers
Answered by
6
refer too the attachment given above
Attachments:
Answered by
1
Question:-
Write a program to print the sum of the series.
S=1-2+3-4+5-6+....N
Code:-
import java.util.*;
class Series
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the total number of terms for series sum: ");
int n=sc.nextInt();
int s=0;
for(int j=1;j<=n;j++)
s+=(j%2!=0)?j:-j;
System.out.println("Sum is: "+s);
}
}
Similar questions