Java Program to Find the Sum of the Series 1 + 2 + 3 + ... + N / Sum of the First N Natural Numbers

Sum of series

Java programs to find the sum of the series $1 + 2 + 3 + ... + n$ i.e. the sum of the first $n$ natural numbers have been shown here. Both the iterative and recursive approaches have been shown below. The sum can also be computed using the mathematical formula $\frac{n(n+1)}{2}$.


Example:

Suppose $n = 6$

So the sum of first $6$ natural numbers = $1 + 2 + 3 + 4 + 5 + 6 = 21$

By using the mathematical formula, we obtain the sum of first $6$ natural numbers = $\frac{6 \cdot (6+1)}{2} = 21 $


The algorithm, pseudocode and time-complexity of the program have also been covered in the following sections.






1. Algorithm to find sum of the series 1 + 2 + 3 + ... + n / Sum of the First N Natural Numbers


1. Take number of terms $n$ as inputs.

2. Set $sum = 0$.

3. Add each $i \in [1, n]$, to $sum$.

4. Display $sum$ as output.




2. Pseudocode to find sum of the series 1 + 2 + 3 + ... + n / Sum of the First N Natural Numbers


Input: Number of terms $n$

Output: Sum of $1+2+3+...+n$

1. Procedure sumSeries($n$):

2. $sum \leftarrow 0$:

3. Repeat for each $i \in [1, n]$:

4. $sum \leftarrow sum + i$

5. Return $sum$

6. End Procedure





3. Time complexity to find sum of the series 1 + 2 + 3 + ... + n / Sum of the First N Natural Numbers


Time Complexity: O(n)

Here $n$ is the number of terms.


Time Complexity (Using formula): O(1)




4. Program & output to find the sum of the series 1 + 2 + 3 + ... + n / Sum of the First N Natural Numbers




4.1. Java Program & output to find sum of the series 1 + 2 + 3 + ... + n / Sum of the First N Natural Numbers using formula

Code has been copied
/********************************************
	    alphabetacoder.com
Java Program to find sum of series 1 + 2 + 3
+...+n using the formula n(n + 1) / 2  
*********************************************/

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // delclare variables
        int n, sum = 0;

        // take no of terms as input
        System.out.print("Enter no of terms: ");
        n = sc.nextInt();

        // calculate the sum using formula
        sum = (n * (n + 1)) / 2;

        // display results
        System.out.println("Sum = " + sum);
    }
}

Output


Enter no of terms: 15

Sum: 120





4.2. Java Program to find sum of the series 1 + 2 + 3 + ... + n / Sum of the First N Natural Numbers using iteration

Code has been copied
/********************************************
	     alphabetacoder.com
Java Program to find sum of series 1 + 2 + 3
+...+n using iterative approach  
*********************************************/

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // delclare variables
        int i, n, sum = 0;

        // take no of terms as input
        System.out.print("Enter no of terms: ");
        n = sc.nextInt();

        // calculate the sum using loop
        for (i = 1; i <= n; i++) {
            sum += i;
        }

        // display results
        System.out.println("Sum = " + sum);
    }
}

Output


Enter no of terms: 5

Sum: 15





4.3. Java Program to find sum of the series 1 + 2 + 3 + ... + n / Sum of the First N Natural Numbers using recursion

Code has been copied
/**********************************
	  alphabetacoder.com
Java Program to find sum of series 
1 + 2 + 3 +...+ n using recursion 
***********************************/

import java.util.Scanner;

class Main {
    // recursive function to calculate sum
    // of series 1 + 2 + 3 ... + n
    int calculate_sum(int n) {
        // exit condition of recursive call
        if (n == 0)
            return 0;

        // call function
        return n + calculate_sum(n - 1);
    }

    public static void main(String[] args) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // delclare object
        Main obj = new Main();

        // delclare variables
        int n, sum;

        // take no of terms as input
        System.out.print("Enter no of terms: ");
        n = sc.nextInt();

        // call function to get result
        sum = obj.calculate_sum(n);

        // display results
        System.out.println("Sum = " + sum);
    }
}

Output


Enter no of terms: 20

Sum: 210