Java program to calculate the sum of series
Problem Statement:
Write a Java program to calculate the sum of the following series:
S = 3 – 6 + 9 – 12 + 15 – 18 …. – 30
Flowchart:
Raptor flow chart for the Java program is shown below:
Java Program:
package series;
/*****************************************************
* Java Program:
* To calculate the series sum shown below
* sum = 3 - 6 + 9 - 12 + .... - 30
******************************************************/
public class SeriesSum {
//////////////////////////////////////////////////
//main method
/////////////////////////////////////////////////
public static void main(String[] args) {
// variables declaration
int sum = 0;
boolean minusSwitch = false;
int term=0;
int endTerm=30;
// for loop
for(int i=3; i<=endTerm; i=i+3) {
if(minusSwitch)
term=-i;
else
term=i;
//accumulating the sum
sum += term;
//toggle the switch
minusSwitch = !minusSwitch ;
} // end loop
System.out.print("Sum of Series S = " + sum);
} // end main
} // end class
Screenshot:

