# C++ Program to Calculate Sum of Natural Numbers

In this example, we will learn to calculate the sum of natural numbers

To understand this example, you should have the knowledge of the following C++ programming topics:

## Natural Numbers

- THe Natural Numbers are the positive integers starting from 1.
- Positive integers 1, 2, 3, 4... are known as natural numbers.
- The 1st program already initialized the number to the variable.
- The 2nd program takes a positive integer from user( suppose user entered
`n`) then, this program displays the value of 1+2+3+....+n.

## Example 1: Program to Find Sum of Natural Numbers using For loop.

```
// calculate the sum of natural numbers using for loop
#include <iostream>
using namespace std;
int main()
{
int n=5, sum = 0, i;
for (i = 1; i <= n; i++) {
sum += i;
}
cout <<"Sum of First" << n << "Natural Numbers is " << sum;
return 0;
}
```

**Output **

Sum of First 5 Natural Numbers is 15

##### Working

- In This program, a For loop is run from 1 to n.
- In each itreration of the loop, the value of i is added to the sum.
- So, the sum of the first n natural numbers is obtained.

## Example 2: Program to Find Sum of Natural Numbers using For loop.

```
// calculate the sum of natural numbers using for loop
// Number Entered By User
#include <iostream>
using namespace std;
int main()
{
int n, sum = 0;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= n; ++i) {
sum += i;
}
cout << "Sum = " << sum;
return 0;
}
```

**Output **

Enter a positive integer: 10 Sum = 55

##### Working

This program assumes that user always enters positive number.

If user enters negative number, **Sum = 0** is displayed and program is terminated.

This program can also be done using recursion. Check out this article for calculating sum of natural numbers using recursion.

## Next Example

We hope that this Example helped you develop better understanding of the concept of "Find Sum of Natural Numbers" in C++.

**Keep Learning : )**

In the next Example, we will learn about C++ `Find Leap Year`

.