# C++ Program to Calculate the Sum of Natural Numbers

## Grammarly

- More than 40 Lakhs Students use Grammarly for their spell and grammar checks. You can register and try for free.

In this post you will learn how to Calculate the Sum of Natural Numbers.

This lesson will teach you how to calculate the sum of natural numbers, with a mathematical function using the C++ Language. Let’s look at the below source code.

## How to Calculate the Sum of Natural Numbers?

## Source Code

#include<iostream> using namespace std; int main() { int n, sum; cin>> n; cout<<" Enter a value : "<< n << endl; sum = n*(n+1)/2; cout<<"\n Sum of first "<< n <<" natural numbers is : "<<sum; return 0; }

## Input

```
5
```

## Output

Enter a value : 5 Sum of first 5 natural numbers is : 15

**#include <iostream>**

- This line which is called the
*header file.*`#include`

statement tells the compiler to use available files and`<iostream>`

is the name of the specific that we have used in this code. The`<iostream>`

file stands for**Input**and**Output**statement.

**using namespace std;**

- The C++ has a
**standard library**that has files for different functions and this line is used to access the standard file for input and output statements.

**int main();**

- This line usually controls the
**function**of the code, as it calls the functions to perform their tasks. - The
`int main()`

shows that the input value is a type of integer, once the program is executed the function returns to the main function, by using the statement**‘return 0;’.**

**{ }**

- The opening
**‘ { ‘**and the closing**‘ } ‘**curly braces mark the start and the finish of the main function - Every statement and value between these braces belong to the main function.

The above statements are the main factors that **support** the function of the source code. Now we can look into the working and layout of the code’s function.

To find the sum of natural numbers we use the following mathematical formula

- First we declare the variable
*n*and s*um*as**integers**. We then collect the number from the user and store it in**n**using function`cin>>`

,display the value using`cout<<`

and the**Insertion Operators'<<‘ , ‘>>’.** - Using the formula
`sum = n*(n+1)/2`

and the value n, we calculate the sum of natural numbers and store it in the integer**sum.** - Using the output statement
`cout`

display the**answer**. - End the function with
`return 0;`

this returns the function to main( ).

**Note:** The **‘ << endl ‘ **in the code is used to **end the current line **and move to the next line and **‘\n’ **is also a **new line function,** to understand how both the functions work exclude it from the code, move it around and work with it.

## Additional Source Code

You can also try the source code given below, the following code is structured using for loop and a mathematical function. Let’s look at the below source code.

#include<iostream> using namespace std; int main() { int n, i, sum=0; cin >> n; cout << "Enter the n value: "<< n << endl; for (i = 1; i <= n; i++) { sum += i; } cout<<"\n Sum of first "<< n <<" natural numbers is : "<<sum; return 0; }

## Input

`5`

## Output

Enter the n value: 5 Sum of first 5 natural numbers is : 15

- In this source code after declaring the
**integers**we will be using and getting the value form the user, we then use a**for loop.**A for loop is used in C++ language to repeat a function continuously until the condition is satisfied. The formula to find the sum of the natural numbers is written in the form of a for loop. The syntax of the for loop isfor (variable initialization; condition; increment operation) { //loop statements; }

- We can work on the for loop in the above source code to understand better
for (i = 1; i <= n; i++) { sum += i; }

- In the for loop first is the
**initialization of the variable**`i = 1`

next the**condition to be satisfied**`i <= n`

which is followed by**increment operator**`i++`

. - The next line in the
*for loop*is the**loop statement**`sum += i`

which is executed after the*for loop*is run. - The loop statement is expanded as
`sum = sum + i`

where the initial value of**sum**is**0**and**i**is**1**. Now when the*for loop*is executed, if the condition is satisfied, i.e**i**is less than or equal to**n**, the value**i**is in incremented by one and the mathematical loop statement is executed. - Now the value of
**sum**changes as per the loop statement function. The execution moves back up to the*for loop*and is executed repeatedly until the value**i**is**not**less than or equal to**n.**Now the function terminates and the last*sum*value is the Sum of n Natural Numbers. - The rest of the statements are similar to find the
**Sum of Natural Numbers.**