Program to the sum of natural numbers

0 Suresh Chand March 28, 2021

In this example, we will learn how to sum natural numbers using the c++ programming language.

To understand this example, You must have knowledge in following topics

To sum the natural number, We will use for loop. We will loop upto the number we want to calculate sum. Let’s look and example to make clear concepts.

#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 = " << sum;
    return 0;
}

The output of above program is

Enter a positive integer: 10
Sum = 55
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Join Our Newsletter Now

Get the latest tutorials, examples and projects updates.

Ads Block Detector Powered by codehelppro.com

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Disable Ad Block

Want Same System