In this post, you will learn *how to Calculate the Sum of Natural Numbers using *** C++**.

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?

RUN CODE SNIPPET## 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.

RUN CODE SNIPPET#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 is

for (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.**