0
0
CppProgramBeginner · 2 min read

C++ Program to Find Sum of N Natural Numbers

To find the sum of n natural numbers in C++, use the formula sum = n * (n + 1) / 2 or a loop to add numbers from 1 to n. For example: int sum = n * (n + 1) / 2; calculates the sum directly.
📋

Examples

Input5
OutputSum of first 5 natural numbers is 15
Input10
OutputSum of first 10 natural numbers is 55
Input1
OutputSum of first 1 natural number is 1
🧠

How to Think About It

To find the sum of the first n natural numbers, think of adding all numbers starting from 1 up to n. You can do this by adding each number one by one using a loop or use the simple math formula n * (n + 1) / 2 which gives the total sum directly without looping.
📐

Algorithm

1
Get input value n from the user
2
Calculate sum using the formula sum = n * (n + 1) / 2
3
Print the sum
💻

Code

cpp
#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter a positive integer: ";
    cin >> n;
    int sum = n * (n + 1) / 2;
    cout << "Sum of first " << n << " natural numbers is " << sum << endl;
    return 0;
}
Output
Enter a positive integer: 5 Sum of first 5 natural numbers is 15
🔍

Dry Run

Let's trace input 5 through the code

1

Input

User enters n = 5

2

Calculate sum

sum = 5 * (5 + 1) / 2 = 5 * 6 / 2 = 30 / 2 = 15

3

Output

Print 'Sum of first 5 natural numbers is 15'

nsum
515
💡

Why This Works

Step 1: Input value

We get the number n from the user which tells how many natural numbers to add.

Step 2: Use formula

The formula n * (n + 1) / 2 quickly calculates the sum without looping.

Step 3: Display result

We print the sum so the user sees the total of the first n natural numbers.

🔄

Alternative Approaches

Using a loop
cpp
#include <iostream>
using namespace std;

int main() {
    int n, sum = 0;
    cout << "Enter a positive integer: ";
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        sum += i;
    }
    cout << "Sum of first " << n << " natural numbers is " << sum << endl;
    return 0;
}
This method uses a loop to add each number one by one, which is easy to understand but slower for very large n.
Using recursion
cpp
#include <iostream>
using namespace std;

int sumNatural(int n) {
    if (n == 1) return 1;
    return n + sumNatural(n - 1);
}

int main() {
    int n;
    cout << "Enter a positive integer: ";
    cin >> n;
    cout << "Sum of first " << n << " natural numbers is " << sumNatural(n) << endl;
    return 0;
}
This method uses recursion to add numbers but can cause stack overflow for very large n.

Complexity: O(1) time, O(1) space

Time Complexity

Using the formula calculates the sum in constant time because it performs only a few arithmetic operations.

Space Complexity

The program uses a fixed amount of memory regardless of input size, so space complexity is constant.

Which Approach is Fastest?

The formula method is fastest and most efficient compared to looping or recursion which take O(n) time.

ApproachTimeSpaceBest For
FormulaO(1)O(1)Fastest calculation for any n
LoopO(n)O(1)Simple logic, easy to understand
RecursionO(n)O(n)Demonstrates recursion but less efficient
💡
Use the formula n * (n + 1) / 2 for the fastest and simplest sum calculation.
⚠️
Forgetting to use integer division or using floating-point division can cause incorrect results.