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 = 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


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


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


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

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

#include<stdio.h>

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

    // take input of n
    printf("Enter no of terms: ");
    scanf("%d", & n);

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

    // display result
    printf("Sum: %d\n", sum);

    return 0;
}

Output


Enter no of terms: 10

Sum: 55





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

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

#include<stdio.h>

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

    // take input of n
    printf("Enter no of terms: ");
    scanf("%d", & n);

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

    // display result
    printf("Sum: %d\n", sum);

    return 0;
}

Output


Enter no of terms: 30

Sum: 465





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

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

#include<stdio.h>

// 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
    printf("Enter no of terms: ");
    scanf("%d", & n);

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

    // display result
    printf("Sum: %d\n", sum);

    return 0;
}

Output


Enter no of terms: 20

Sum: 210