C++ Programming Code Examples C++ > Mathematics Code Examples Program to Calculate Sum of Natural Numbers Program to Calculate Sum of Natural Numbers In this example, you'll learn to calculate the sum of natural numbers. Positive integers 1, 2, 3, 4... are known as natural numbers. This program takes a positive integer from user( suppose user entered n ) then, this program displays the value of 1+2+3+....+n. This program assumes that user always enters positive number. If user enters negative number, Sum = 0 is displayed and program is terminated. #include <iostream> using namespace std; int main() { int n, sum = 0; cout << "Enter a positive integer: "; cin >> n; for (int j = 1; j <= n; ++j) { sum += j; } cout << "Sum = " << sum; return 0; }