# C++ Program to find Sum of Natural Numbers

Write a C++ Program to find the sum of natural numbers from 1 to n. This C++ program allows you to enter the maximum number to calculate the sum of natural numbers. Next, we used the while loop to iterate from 1 to n by incrementing the i value. Within the loop, we are adding each value of i to the sum and prints the sum.

```#include<iostream>

using namespace std;

int main()
{
int number, i = 1, sum = 0;

cout << "\nPlease Enter the Number to find Sum of Natural Num  =  ";
cin >> number;

while( i <= number)
{
sum = sum + i;
i++;
}

cout << "\nThe Sum of Natural Number from 1 to " << number << " = " << sum;

return 0;
}```
``````Please Enter the Number to find Sum of Natural Num  =  10

The Sum of Natural Number from 1 to 10 = 55``````

C++ Sum of Natural Numbers example code using Do While Loop.

```#include<iostream>

using namespace std;

int main()
{
int number, i = 1, sum = 0;

cout << "\nPlease Enter the Number to find Sum of Natural Num  =  ";
cin >> number;

do
{
sum = sum + i;
i++;
} while( i <= number);

cout << "\nThe Sum of Natural Number from 1 to " << number << " = " << sum;

return 0;
}```
``````Please Enter the Number to find Sum of Natural Num  =  20

The Sum of Natural Number from 1 to 20 = 210``````

## C++ Program to find Sum of Natural Numbers using For Loop

Here, we added one more cout statement inside the for loop: cout << “\n Iteration = ” << i << “, i = ” << i << “, sum = “<< sum << ” + ” << i << ” = “<< sum + i;. This statement helps you to understand the program in iteration wise.

```#include<iostream>

using namespace std;

int main()
{
int number, i = 1, sum = 0;

cout << "\nPlease Enter the Number to find Sum of Natural Num  =  ";
cin >> number;

for(i = 1; i <= number; i++)
{
cout << "\n Iteration = " << i << ", i = " << i << ", sum = "<< sum << " + " << i << " = "<< sum + i;
sum = sum + i;

}

cout << "\nThe Sum of Natural Number from 1 to " << number << " = " << sum;

return 0;
}```

This Program helps to calculate the Sum of Natural Numbers using Functions.

```#include<iostream>

using namespace std;

int sumOfNaturalNumbers(int number)
{
int sum = 0;

for(int i = 1; i <= number; i++)
{
sum = sum + i;

}
return sum;
}
int main()
{
int number;

cout << "\nPlease Enter the Number to find Sum of Natural Num  =  ";
cin >> number;

int sum = sumOfNaturalNumbers(number);

cout << "\nThe Sum of Natural Number from 1 to " << number << " = " << sum;

return 0;
}```
``````Please Enter the Number to find Sum of Natural Num  =  25

The Sum of Natural Number from 1 to 25 = 325``````

## C++ Program to Calculate the Sum of Natural Numbers using a Math formula

As we all know, the mathematical formula to calculate the sum of natural numbers = number * (number + 1) / 2).

```#include<iostream>

using namespace std;

int sumOfNaturalNumbers(int number)
{

if(number == 0)
{
return number;
}
else
{
return (number * (number + 1) / 2);
}
}
int main()
{
int number;

cout << "\nPlease Enter the Number to find Sum of Natural Num  =  ";
cin >> number;

int sum = sumOfNaturalNumbers(number);

cout << "\nThe Sum of Natural Number from 1 to " << number << " = " << sum;

return 0;
}```
``````Please Enter the Number to find Sum of Natural Num  =  50

The Sum of Natural Number from 1 to 50 = 1275``````

C++ Program to Calculate the Sum of Natural Numbers using Recursion.

```#include<iostream>

using namespace std;

int sumOfNaturalNumbers(int number)
{

if(number == 0)
{
return number;
}
else
{
return (number + sumOfNaturalNumbers(number - 1));
}
}
int main()
{
int number;

cout << "\nPlease Enter the Number to find Sum of Natural Num  =  ";
cin >> number;

int sum = sumOfNaturalNumbers(number);

cout << "\nThe Sum of Natural Number from 1 to " << number << " = " << sum;

return 0;
}```
``````Please Enter the Number to find Sum of Natural Num  =  90

The Sum of Natural Number from 1 to 90 = 4095``````