# Sum of First N Natural Numbers in C

«
»
Problem Description:

Write a C program to find the sum of first n natural numbers.

What is a Natural Number?

Natural numbers are all positive integers ranging from 1 to n or infinity. Sum of first n natural number, for any number n, sum is defined as 1+2+3—-n, which is an arithmetic series whose sum is (n * (n + 1))/2. For example, If n=3, sum of the first 3 natural numbers is 1 + 2 + 3 = 6.

Formula:

Formula to calculate the Sum of First N Natural Numbers are (n * (n + 1))/2.

Example:
If n=4, sum of the first 4 natural numbers is
=> (n * (n + 1))/2
=> (4 * (4 + 1))/2
=> 20/2
=> 10

Problem Solution

1. Take a number as input.
2. Find the sum of first n natural numbers using loops, function and formula.
3. Print the final result and exit.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

Sum of First N Natural Numbers in C can be found out in following ways:

Method 1: (Using For Loop)

In this approach, we find the sum of the first n natural number using a for loop and in each iteration, and add ‘i’ to the sum, where i is the variable which is used to initialize the loop and sum is the variable which stores the sum of n natural numbers.

Example:

Let n = 4
Initially sum = 0,
Iterate from i=1 to i=4 using a for loop, and in each iteration add i to sum.
So sum = 1+2+3+4 = 10.

Algorithm:

Step 1: Start the program.
Step 2: Input the value of n.
Step 3: Initialize a for loop from 1 to n.
Step 4: Print sum of first n natural numbers.
Step 5: End the Program.

Program/Source Code

Here is source code of the C program to calculate the sum of first N natural numbers using for loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

1. `/*`
2. ` * C program to find the sum of 'N' natural numbers using for loop`
3. ` */`
4. `#include <stdio.h>`
5. ` `
6. `int main()`
7. `{`
8. `    int n;`
9. `    printf("Enter number:");`
10. `    scanf("%d",&n);`
11. `    int sum=0;`
12. `    for(int i=1;i<=n;i++)`
13. `    sum += i;`
14. `    printf("sum of first %d numbers is: %d",n,sum);`
15. `    return 0;`
16. `}`
Program Explanation

1. Input a number n
2. Initialize a loop from 1 to n using ‘i‘ and in each iteration add the value of ‘i‘ in the sum variable.
3. Print the sum.

Time Complexity: O(n)
In the above program, a loop is executed from 1 to n, so time complexity is O(n).

Space Complexity: O(1)
Since no auxiliary space is required, space complexity is O(1).

Run Time Testcases

To find the sum of the first n natural numbers, we enter the value “4” as input.

```Enter number: 4
sum of first 4 numbers is: 10```

Method 2: (Using While Loop)

In this approach, we find the sum of the first n natural number using a while loop. Initially i=1, execute loop until i<=n and in each iteration add i to sum and increment i.

Example:

Let n = 5
Initially, sum = 0 and i = 1
Iterate until i<=5 using a while loop and in each iteration add i to sum.

So sum = 1+2+3+4+5=15

Algorithm:

Step 1: Start the program.
Step 2: Input the value of n.
Step 3: Initialize a variable i and run a while loop from 1 to n.
Step 4: Print sum of first n natural numbers.
Step 5: End the Program.

Program/Source Code

Here is source code of the C program to calculate the sum of first N natural numbers using while loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

1. `/*`
2. ` * C program to find the sum of 'N' natural numbers using while loop`
3. ` */`
4. `#include <stdio.h>`
5. ` `
6. `int main()`
7. `{`
8. `    int n;`
9. `    printf("Enter number:");`
10. `    scanf("%d",&n);`
11. `    int sum=0,i=1;`
12. `    while(i<=n)`
13. `    sum += i++;`
14. `    printf("sum of first %d numbers is: %d",n,sum);`
15. `    return 0;`
16. `}`
Program Explanation

1. Input a number n
2. Initialize a variable i=1 and sum=0.
3. After that iterate until i is less than n using a while loop and in each iteration add i to the sum.
4. After the loop is executed, print the sum.

Time Complexity: O(n)
In the above program, a loop is executed from 1 to n, so time complexity is O(n).

Space Complexity: O(1)
Since no auxiliary space is required, space complexity is O(1).

Run Time Testcases

To find the sum of the first n natural numbers, we enter the value “5” as input.

```Enter number: 5
sum of first 5 numbers is: 15```

Method 3: (Using Do While Loop)

In this approach, we find the sum of the first n natural number using a do while loop. Initialize a variable i and run a Do while loop from 1 to n. After the loop is executed, sum contains the sum of first n natural numbers.

Example:

Let n=6
Initially, sum = 0 and i=1,
Iterate until i<=6 using a do while loop and in each iteration add i to sum.

So sum = 1+2+3+4+5+6=21

Program/Source Code

Here is source code of the C program to calculate the sum of first N natural numbers using do while loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

1. `/*`
2. ` * C program to find the sum of 'N' natural numbers using do while loop`
3. ` */`
4. `#include <stdio.h>`
5. ` `
6. `int main()`
7. `{`
8. `    int n;`
9. `    printf("Enter number:");`
10. `    scanf("%d",&n);`
11. `    int sum=0,i=1;`
12. `    do`
13. `    {`
14. `        sum += i++;`
15. `    }`
16. `    while(i<=n);`
17. `    printf("sum of first %d numbers is: %d",n,sum);`
18. `    return 0;`
19. `}`
Program Explanation

1. Input a number n
2. Initialize a variable i=1 and sum=0.
3. After that iterate until i is less than n using a do while loop and in each iteration add i to the sum.
4. After the loop is executed, print the sum.

Time Complexity: O(n)
In the above program, a loop is executed from 1 to n, so time complexity is O(n).

Space Complexity: O(1)
Since no auxiliary space is required, space complexity is O(1).

Run Time Testcases

To find the sum of the first n natural numbers, we enter the value “6” as input.

```Enter number: 6
sum of first 6 numbers is: 21```

Method 4: (Using Formula)

For any integer n, the sum is 1+2+3 —— n. This corresponds to an arithmetic series whose sum is (n *(n+1))/2.

So in this approach, sum is calculated using the formula: (n *(n+1))/2.

Example:

If n=7, sum = (7 * (7 + 1))/2 = 56/2 = 28
If n=3, sum = (3 * (3 + 1))/2 = 12/2 = 6

Program/Source Code

Here is source code of the C program to calculate the sum of first N natural numbers using formula. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

1. `/*`
2. ` * C program to find the sum of 'N' natural numbers using formula`
3. ` */`
4. `#include <stdio.h>`
5. ` `
6. `int main()`
7. `{`
8. `    int n;`
9. `    printf("Enter number:");`
10. `    scanf("%d",&n);`
11. `    int sum= ((n)*(n+1))/2;`
12. `    printf("sum of first %d numbers is: %d",n,sum);`
13. `    return 0;`
14. `}`
Program Explanation

1. Input a number n
2. Now calculate sum of first n natural numbers by using the formula (n * (n + 1))/2 and store it in a variable sum.
3. Print the value of sum.

Time Complexity: O(1)
In the above program, only basic input output operations are performed, so time complexity is O(1).

Space Complexity: O(1)
Since no auxiliary space is required, space complexity is O(1).

Run Time Testcases

To find the sum of the first n natural numbers, we enter the value “7” as input.

```Enter number: 7
sum of first 7 numbers is: 28```

Method 5: (Using Recursion)

The prototype of the recursive function is: int sum(int n).

Following is the function definition:

```int  sum(int n)
{
if(n==1)
return 1;
else
return n + sum(n-1);
}```

Example:

Now let n=5; So function calls would be as follows.

```sum(5) =  5 + sum(4)  —— ——— ——— ——— ——— ———     15
sum(4) = 4 + sum(3) ——— ——— ————— ——  10
sum (3) = 3 + sum(2)  ——— ——— —-   6
sum(2) = 2 + sum(1)  ——— 3
sum(1) = 1  // Base Case```

Algorithm:

Step 1: Start the program.
Step 2: Input the value of n.
Step 3: Declare a function func to find sum.

• Base case: When n=1,return 1
• Other cases: return n + func(n-1)

Step 4: Print the value of sum as returned by the func.
Step 5: End the Program

Program/Source Code

Here is source code of the C program to calculate the sum of first N natural numbers using recursion. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

1. `/*`
2. ` * C program to find the sum of 'N' natural numbers using Recursion.`
3. ` */`
4. `#include <stdio.h>`
5. ` `
6. `int sum(int n)`
7. `{`
8. `    //Base Case`
9. `    if(n==1)`
10. `    return 1;`
11. `    return n + sum(n-1);`
12. `}`
13. ` `
14. `int main()`
15. `{`
16. `    int n;`
17. `    printf("Enter number: ");`
18. `    scanf("%d",&n);`
19. `    printf("Sum of first %d natural number is: %d",n,sum(n));`
20. `    return 0;`
21. `}`
Program Explanation

1. Input a number n
2. Declare a function sum which takes ‘n‘ as the parameter with base condition to stop when n is equal to 1.
3. Print the value returned by function as the sum.

Time Complexity: O(n)
The program above has n function calls, so the time complexity is O(n).

Space Complexity: O(1)
Since no auxiliary space is required, space complexity is O(1).

Run Time Testcases

To find the sum of the first n natural numbers, we enter the value “5” as input.

```Enter number: 5
sum of first 5 numbers is: 15```

Method 6: (Using Function)

The prototype of the recursive function is: int sum_n(int n). It takes n as the parameter and returns the sum of first n natural numbers.

Algorithm:

Step 1: Start the program.
Step 2: Input the value of n.
Step 3: Declare a function func to find sum.
Step 4: Print the value of sum as returned by the func.
Step 5: End the Program.

Program/Source Code

Here is source code of the C program to calculate the sum of first N natural numbers using function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

1. `/*`
2. ` * C program to find the sum of 'N' natural numbers using function.`
3. ` */`
4. `#include <stdio.h>`
5. ` `
6. `int sum_n(int n)`
7. `{`
8. `    int s=0;`
9. `    for(int i=1;i<=n;i++)`
10. `    s+=i;`
11. `    return s;`
12. `}`
13. `int main()`
14. `{`
15. `    int n;`
16. `    printf("Enter n:");`
17. `    scanf("%d",&n);`
18. `    printf("sum of first %d numbers is: %d",n,sum_n(n));`
19. `    return 0;`
20. `}`
Program Explanation

1. Input a number n
2. Declare a function sum which takes ‘n‘ and return the sum of first n natural numbers.
3. Print the value returned by function sum.

Time Complexity: O(n)
The program above has n function calls, so the time complexity is O(n).

Space Complexity: O(1)
Since no auxiliary space is required, space complexity is O(1).

Run Time Testcases

To find the sum of the first n natural numbers, we enter the value “120” as input.

```Enter number: 15
sum of first 15 numbers is: 120```

Method 7: (Using Arrays)

In this approach, we store the natural numbers in an array then find the sum of the array which is equal the sum of first n natural numbers.

Example:

If n=5;
So array of first 5 natural number is: [1,2,3,4,5]

Sum of array = 1+2+3+4+5=15;

Sum of first 5 natural numbers=sum of array=15

Program/Source Code

Here is source code of the C program to calculate the sum of first N natural numbers using array. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

1. `/*`
2. ` * C program to find the sum of 'N' natural numbers using array.`
3. ` */`
4. `#include <stdio.h>`
5. ` `
6. `int main()`
7. `{`
8. `    int n;`
9. `    printf("Enter number:");`
10. `    scanf("%d",&n);`
11. `    int arr[n];`
12. ` `
13. `    // Store n natural numbers in an array`
14. `    for(int i=0;i<n;i++)`
15. `    arr[i] = i +1 ;`
16. `    int sum=0;`
17. `    for(int i=0;i<n;i++)`
18. `    sum+=arr[i];`
19. `    printf("sum of first %d numbers is: %d",n,sum);`
20. `    return 0;`
21. `}`
Program Explanation

1. Input a number n
2. Store first n natural numbers in an array.
3. Find the sum of the array and store it in sum.
4. Print the value of sum.

Time Complexity: O(n)
In the above program, loop is executed from 1 to n, so time complexity is O(n).

Space Complexity: O(1)
Since no auxiliary space is required, space complexity is O(1).

Run Time Testcases

To find the sum of the first n natural numbers, we enter the value “5” as input.

```Enter number: 5
sum of first 5 numbers is: 15```

Method 8: Sum of N Natural Numbers in a Given Range

In this approach we traverse between the intervals using the iterative statement to find the sum of natural numbers in the interval.

Example:

Let low = 2; high = 9;

So sum = 2+3+4+5+6+7+8+9 (from low to high)

sum = 44.

Program/Source Code

Here is source code of the C program to calculate the sum of N natural numbers in a Given Range. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

1. `/*`
2. ` * C program to find the sum of 'N' natural numbers in a Given Range.`
3. ` */`
4. `#include <stdio.h>`
5. ` `
6. `int main()`
7. `{`
8. `    int l,r;`
9. `    printf("Enter ends of interval to find sum:");`
10. `    scanf("%d %d",&l,&r);`
11. `    int sum=0;`
12. `    for(int i=l;i<=r;i++)`
13. `    sum +=i;`
14. `    printf("Sum is:  %d",sum);`
15. `    return 0;`
16. `}`
Program Explanation

1. Input the two ends of the interval i.e high and low.
2. Initialize sum = 0 and initiate a loop from low to high using i.
3. Add i to sum in each iteration.
4. Then Print the sum variable.

Time Complexity: O(n)
In the above program, since there is a loop executed from low to high, the time complexity is linear.

Space Complexity: O(1)
Since no auxiliary space is required, space complexity is O(1).

Run Time Testcases

To find the sum of n natural numbers in a given range, we enter the values ​​low = “2” and high = “9” as input.

```Enter ends of interval to find sum:2 9
Sum is:  44```

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.