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

Sum of series

C++ 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 = 5$

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

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


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






1. Algorithm to find the sum of the series 1 + 2 + 3 + ... + n


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 the sum of the series 1 + 2 + 3 + ... + n


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 the sum of the series 1 + 2 + 3 + ... + n


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




4.1. C++ Program & output to find the sum of the series 1 + 2 + 3 + ... + n using formula

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

#include<iostream>

using namespace std;

int main() {
    // delclare variables
    int n, sum = 0;

    // take input of n
    cout << "Enter no. of terms: ";
    cin >> n;

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

    // display result
    cout << "Sum: " << sum << endl;

    return 0;
}

Output


Enter no of terms: 5

Sum: 15





4.2. C++ Program to find the sum of the series 1 + 2 + 3 + ... + n using iteration

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

#include<iostream>

using namespace std;

int main() {
    // delclare variables
    int i, n, sum = 0;

    // take input of n
    cout << "Enter no of terms: ";
    cin >> n;

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

    // display result
    cout << "Sum: " << sum << endl;

    return 0;
}

Output


Enter no of terms: 30

Sum: 465





4.3. C++ Program to find the sum of the series 1 + 2 + 3 + ... + n using recursion

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

#include<iostream>

using namespace std;

// 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);
}

int main() {
    // delclare variables
    int n, sum;

    // take input of n
    cout << "Enter no of terms: ";
    cin >> n;

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

    // display result
    cout << "Sum: " << sum << endl;

    return 0;
}

Output


Enter no of terms: 10

Sum: 55