Pin It

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

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 →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot