Write a program to calculate and print the sum of the following series: Sum(x) = 2 – 4 + 6 – 8 + 10 - 12 … - 100

 Tweet Program to demonstrate how to calculate and print the sum of the following series: Sum(x) = 2 – 4 + 6 – 8 + 10 - 12 … - 100 in Java. package com.hubberspot.java.series.problems; public class Series { public static void main(String[] args) { int sumOfSeries = 0; int i = 2; while( i <= 100 ) { if(i % 4 == 0) { sumOfSeries = sumOfSeries - i; } else { sumOfSeries = sumOfSeries + i; } i = i + 2; } System.out.println("Sum of the series : " + sumOfSeries); } } Output of the program :
Program to demonstrate how to calculate and print the sum of the following series:
Sum(x) = 2 – 4 + 6 – 8 + 10 - 12 … - 100 in Java.

package com.hubberspot.java.series.problems;

public class Series {

public static void main(String[] args) {

int sumOfSeries = 0;

int i = 2;

while( i <= 100 ) {

if(i % 4 == 0) {

sumOfSeries = sumOfSeries - i;

}
else {

sumOfSeries = sumOfSeries + i;

}

i = i + 2;
}

System.out.println("Sum of the series : " + sumOfSeries);

}

}

Output of the program :

Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →