C++ Program to Find Sum of Natural Number

In this program, you'll learn how to find sum of natural numbers. User have to enter the positive integer.



Source Code
#include <iostream>
using namespace std;

int main( )
{
    int n, i, sum = 0;
    cout<<"Enter a positive integer: ";
    cin>> n;
    for(i=1; i <= n; ++i)
    {
        sum = sum + i;
    }
    cout<<"Sum = " << sum;
    return 0;
}
Output
Enter a positive integer: 100
Sum = 5050