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.
C++ Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#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; } |
Output: