# C++ Program to Calculate Sum of Natural Numbers

## Source Code

``````#include <iostream>

using namespace std;

int main()
{
int n; // Declare a variable to hold the number of natural numbers
int sum = 0; // Declare a variable to hold the sum and initialize it to 0

cout << "Enter the number of natural numbers: "; // Prompt the user to enter the number of natural numbers
cin >> n; // Read the number from the standard input stream

// Calculate the sum of the first n natural numbers
for (int i = 1; i <= n; i++)
sum += i;

cout << "The sum of the first " << n << " natural numbers is: " << sum << endl; // Print the sum

return 0;
}
``````

## Code Explanation

This program declares a variable `n` of type `int` to hold the number of natural numbers, and a variable `sum` of type `int` to hold the sum of the natural numbers. It then uses the `cout` object to print a prompt asking the user to enter the number of natural numbers, and the `cin` object to read the number from the standard input stream.

To calculate the sum of the natural numbers, the program uses a `for` loop to iterate over the range `[1, n]` and add each number to the sum. The loop variable `i` is initialized to 1, and the loop continues as long as `i` is less than or equal to `n`. The loop variable is incremented by 1 on each iteration.

Finally, the program uses the `cout` object to print the value of `sum`, which is the sum of the first `n` natural numbers.

## What is a Natural Number?

In mathematics, a natural number is a positive integer, that is, a number that is greater than 0. The set of natural numbers is represented by the symbol `N` and is defined as:

```Copy code```N = {1, 2, 3, 4, 5, ...}
``````

The natural numbers are also known as the counting numbers or the positive integers. They are used to represent the number of objects in a set, and are often used to count the number of elements in a list or array.

The natural numbers are usually denoted by the letters `n`, `m`, or `k`, and are often used as indices or loop variables in programming.

Note: C++ All Examples