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

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

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.

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

- Sum of First N Natural Numbers in C using For Loop
- Sum of First N Natural Numbers in C using While Loop
- Sum of First N Natural Numbers in C using Do While Loop
- Sum of First N Natural Numbers in C using Formula
- Sum of First N Natural Numbers in C using Recursion
- Sum of First N Natural Numbers in C using Function
- Sum of First N Natural Numbers in C using Arrays
- Sum of N Natural Numbers in a Given Range

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.

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.

`/*`

`* C program to find the sum of 'N' natural numbers using for loop`

`*/`

`#include <stdio.h>`

int main()

`{`

int n;

printf("Enter number:");

scanf("%d",&n);

int sum=0;

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

sum += i;

printf("sum of first %d numbers is: %d",n,sum);

return 0;

`}`

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).

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

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.

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.

`/*`

`* C program to find the sum of 'N' natural numbers using while loop`

`*/`

`#include <stdio.h>`

int main()

`{`

int n;

printf("Enter number:");

scanf("%d",&n);

int sum=0,i=1;

while(i<=n)

sum += i++;

printf("sum of first %d numbers is: %d",n,sum);

return 0;

`}`

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).

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

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

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.

`/*`

`* C program to find the sum of 'N' natural numbers using do while loop`

`*/`

`#include <stdio.h>`

int main()

`{`

int n;

printf("Enter number:");

scanf("%d",&n);

int sum=0,i=1;

`do`

`{`

sum += i++;

`}`

while(i<=n);

printf("sum of first %d numbers is: %d",n,sum);

return 0;

`}`

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).

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

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

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.

`/*`

`* C program to find the sum of 'N' natural numbers using formula`

`*/`

`#include <stdio.h>`

int main()

`{`

int n;

printf("Enter number:");

scanf("%d",&n);

int sum= ((n)*(n+1))/2;

printf("sum of first %d numbers is: %d",n,sum);

return 0;

`}`

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).

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

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

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.

`/*`

`* C program to find the sum of 'N' natural numbers using Recursion.`

`*/`

`#include <stdio.h>`

int sum(int n)

`{`

`//Base Case`

if(n==1)

return 1;

return n + sum(n-1);

`}`

int main()

`{`

int n;

printf("Enter number: ");

scanf("%d",&n);

printf("Sum of first %d natural number is: %d",n,sum(n));

return 0;

`}`

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).

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

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.

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.

`/*`

`* C program to find the sum of 'N' natural numbers using function.`

`*/`

`#include <stdio.h>`

int sum_n(int n)

`{`

int s=0;

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

s+=i;

return s;

`}`

int main()

`{`

int n;

printf("Enter n:");

scanf("%d",&n);

printf("sum of first %d numbers is: %d",n,sum_n(n));

return 0;

`}`

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).

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

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

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.

`/*`

`* C program to find the sum of 'N' natural numbers using array.`

`*/`

`#include <stdio.h>`

int main()

`{`

int n;

printf("Enter number:");

scanf("%d",&n);

int arr[n];

`// Store n natural numbers in an array`

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

arr[i] = i +1 ;

int sum=0;

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

sum+=arr[i];

printf("sum of first %d numbers is: %d",n,sum);

return 0;

`}`

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).

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

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.

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.

`/*`

`* C program to find the sum of 'N' natural numbers in a Given Range.`

`*/`

`#include <stdio.h>`

int main()

`{`

int l,r;

printf("Enter ends of interval to find sum:");

scanf("%d %d",&l,&r);

int sum=0;

for(int i=l;i<=r;i++)

sum +=i;

printf("Sum is: %d",sum);

return 0;

`}`

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).

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”.

**Next Steps:**

- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

**Related Posts:**

- Buy C Books
- Buy Computer Science Books
- Apply for Computer Science Internship
- Apply for C Internship
- Watch Advanced C Programming Videos