# C++ Program to Find the Sum of N Natural Numbers

## Find the Sum of N Natural Numbers in C++

Given an integer input the objective is to write a code to Find the Sum of N Natural Numbers in C++. To do so we simply keep adding the value of the iter variable using a for loop.

```Example
Input : num = 8Output : 36

Where first 8 number is 1, 2, 3, 4, 5, 6, 7, 8
Sum of numbers = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36``` ## Find the Sum of N Natural Numbers in C++

Given an integer input of N, the objective is to find the sum of all the natural numbers until the given input integer. To do so we can use different approaches to write the C++ code and some such methods are mentioned below,

• Method 1: Using for Loop
• Method 2: Using Formula for the Sum of Nth Term
• Method 3: Using Recursion

## Method 1 : Using for Loop

In this method we’ll add all the natural numbers until the given integer input using for loop in C++.

### C++ Code

Run
```#include<bits/stdc++.h>
using namespace std;

int main()
{
int n;
cout << "Enter a number : ";
cin >> n;

int sum=0;

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

cout << sum;

return 0;
}
```

### Output

`Enter a number : 1055`

### Working

For a user input num.

1.  Initialize a variable sum = 0.
2. Using a for loop in iteration ‘i’ iterate between [1, num].
3. Each time add ‘i’ to current sum as sum = sum + i.
4. Print sum.

### Explanation

Given an integer input N, the objective is to calculate the sum of all the natural numbers until the integer N. To do so we iterate through all the numbers that lay within N and keep incrementing the sum value.

The algorithm for the above code is as follows,

1.  Include the required header files using the include keyword.
2. In the main() function initialize the required variables.
3. Run a for loop with range as N+1.
4. Keep adding the iter values to the Sum variable.
5. Print Sum variable using cout keyword.

The output for the above mentioned code is the sum of all the natural numbers until the given value.

## Method 2 : Using Formula for the Sum of Nth Term

In this Method we use the formula for finding the sum of N term.

### C++ Code

Run
```#include<bits/stdc++.h>
using namespace std;

int main()
{
int n;

cout << "Enter a number : ";
cin >> n;

cout << n*(n+1)/2;

return 0;
}
```

### Output

`Enter a number : 1055`

### Working

For a user input n.

• Step 1: Initialize a variable sum = 0.
• Step 2: Use formula sum = n(n+1)/2
• Step 3: Print sum

### Explanation

Given an integer input N, the objective is to calculate the sum of all the natural numbers until the integer N. To do so we iterate through all the numbers that lay within N and keep incrementing the sum value.

The algorithm for the above code is as follows,

1.  Include the required header files using the include keyword.
2. In the main() function initialize the required variables.
3. Run a for loop with range as N+1.
4. Keep adding the iter values to the Sum variable.
5. Print Sum variable using cout keyword.

This algorithm uses the formula n(n+1)/2 that can be used to find sum of first N natural numbers. This also reduces the time complexity from O(n) to O(1). The output for the above mentioned code is the sum of all the natural numbers until the given value.

## Method 3 : Using Recursion

This method uses Recursion to recursively add the natural numbers up to the given integer input using recursion in c++.

### C++ Code

Run
```#include<bits/stdc++.h>
using namespace std;

int getSum(int n)
{
if(n==0)
return n;

return n + getSum(n-1);
}

int main()
{
int n;
cout << "Enter a number : ";
cin >> n;

int sum = getSum(n);

cout << sum;

return 0;
}
```

### Output

`Enter a number : 1055`

### Working

For a user input n.

1. Initialize a variable sum = 0.
2. Call function getSum (n).
3. In each recursive call add the current value of n and call for lower recursion call using return n + getSum(n-1);
4. Print sum value

### Explanation

Given an integer input N, the objective is to calculate the sum of all the natural numbers until the integer N. To do so we recursively call a function  iterate through all the numbers that lay within N and keep incrementing the sum value.

The algorithm for the above code is as follows,

1.  Include the required header files using the include keyword.
2. Define a Recursive function getSum() which takes the number input as an argument.
3. Recursively call the function and keep on adding the return statements.
4. In the main() function initialize the required variables.
5. Call the Recursive function and print out the returned value using cout keyword.

The output for the above mentioned code is the sum of all the natural numbers until the given value.

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

## Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others