Back to: C++ Tutorials For Beginners and Professionals

**Sum of N Natural Numbers using Loop in C++**

In this article, I am going to discuss the Program to Print **Sum of N Natural Numbers using Loop in C++** with Examples. Please read our previous articles, where we discussed **Multiplication Table for a Given Number in C++** with Examples.

**Sum of N Natural Numbers:**

So let us understand what is the sum of â€˜Nâ€™ natural numbers. Natural numbers start from â€˜1â€™ onwards.

The sum of first â€˜7â€™ natural number is: 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. There is direct formula for that,

Above is the direct formula to find the sum of first â€˜nâ€™ natural numbers but here we will not use that, we will find the sum by using for loop. So, we want to add the numbers one by one and find the sum. Before writing the program let us do some paperwork.

- first, we add â€˜1â€™ with zero because there is no other number, so the result is â€˜1â€™.
- Now, add â€˜2â€™ to the previous result that is â€˜1 + 2 = 3â€™.
- Now, again add â€˜3â€™ to the result of the previous addition â€˜3 + 3 = 6â€™.
- Add â€˜4â€™ to the previous result that is â€˜6 + 4 = 10â€™.
- Add â€˜5â€™ to the result of the previous addition that is â€™10 + 5 = 15â€™
- Add â€˜6â€™ to previous result that is â€™15 + 6 = 21â€™

So, the sum of the first six natural numbers is 21. So, thatâ€™s how we can find the sum without using the formula. Now where to store the result of the addition? We will create a separate variable for this and keep modifying this again and again in each step. Letâ€™s say the name of the variable is â€˜sumâ€™.

On the left-hand side, we are written numbers from 1 to 6. So, it is looking like a counter and these numbers are added to some another number in every step.

**Sum of N Natural Numbers Flowchart:**

So already we have written a flow chart here. Let us study that flow chart. First, we take a number from the user. Then we should initialize the counter to â€˜1â€™ as well as our â€˜sumâ€™ variable to 0 which will be modified in every iteration of the loop. Then up to where we want to find the sum, let’s take that number be â€˜nâ€™. So, we will check the condition as â€˜if (i <= n)â€™. If our counter is equal to the given number then the loop will be exit.

If the condition will true then modify the â€˜sumâ€™ variable as â€˜sum = sum + iâ€™ and increment the â€˜iâ€™ variable by 1. After this step control goes back to the condition and checks if the condition is true then again modify the â€˜sumâ€™ and â€˜iâ€™ variables and it will continue until the condition become false. If the condition will false, then simply stop the loop. Now let us look at the program.

**Program to print sum of first N natural numbers using for loop in C++:**

#include <iostream> using namespace std; int main() { int n, sum = 0; cout << "Enter number:" << endl; cin >> n; cout << endl; for (int i = 1; i <= n; i++) { sum += i; } cout << "Sum N no. is " << sum << endl; return 0; }

**Output:**

**Program to print sum of first N natural numbers using while loop in C++:**

#include <iostream> using namespace std; int main() { int n, i = 1, sum = 0; cout << "Enter number:" << endl; cin >> n; cout << endl; while (i <= n) { sum += i; i++; } cout << "Sum N no. is " << sum << endl; return 0; }

**Output:**

In the next article, I am going to discuss the **Factorial of a Number using Loop in C++** with examples. Here, in this article, I try to explain the **Sum of N natural numbers using Loop in C++** with examples. I hope you enjoy this program to print the sum of N natural numbers using a loop in C++ article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

**About the Author: Pranaya Rout**

Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.