Natural number
Natural numbers are numbers that are common and clear in nature. As such, it is a whole, non-negative number.
Logic
To print the sum of first N natural numbers, we need to run one loop from 1 to N, and each time inside the loop we have to add / sum value of “i” (current number) into one temporary variable.
Once the loop is over, outside of the loop we have to print a temporary variable containing the sum of natural numbers.
See also: Print first N natural numbers using for loop
Program
#include <iostream> using namespace std; int main() { int i, num, sum = 0; // Take number from user cout << "Enter any number : "; cin >> num; for(i = 1; i <= num; i++) { sum += i; } cout << endl << "Sum of first " << num << " natural numbers : " << sum; return 0; }
Output
Enter any number : 50
Sum of first 50 natural numbers = 1275