Java Program to print n terms of series 1, (1+2),(1+2+3), .....

Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + …… + (1+2+3+4+…+n)



Given the value of n, we need to find the sum of the series where i-th term is sum of first i natural numbers.
Examples :
Input  : n = 5   
Output : 35
Explanation :
(1) + (1+2) + (1+2+3) + (1+2+3+4) + (1+2+3+4+5) = 35

Input  : n = 10
Output : 220
Explanation :
(1) + (1+2) + (1+2+3) +  .... +(1+2+3+4+.....+10) = 220

// JAVA Code For Sum of the series 


import java.util.*; 


  


class GFG {     


    // Function to find sum of given series 


    static int sumOfSeries(int n) 


    { 
        int sum = 0; 


        for (int i = 1 ; i <= n ; i++) 


            for (int j = 1 ; j <= i ; j++) 


                sum += j; 


        return sum; 


    } 
  
   /* Driver program to test above function */

    public static void main(String[] args)  
    { 
         int n = 10; 
         System.out.println(sumOfSeries(n));           


    } 

Post a Comment

Previous Post Next Post