C++ Program to Find the Sum of First N Natural Numbers

Problem

Write a program in C++ to find the sum of first “N” natural numbers and display it on the console.

Solution

Here’s a sample code snippet demonstrating how you display the sum of N natural numbers in C++.

#include<iostream>
using namespace std;

int main()
{
   int n, natural, sum=0;
   cout<<"Enter the Limit: ";
   cin>>n;
   for(natural=1; natural<=n; natural++)
      sum = sum+natural;
   cout<<"\nSum of "<<n<<" Natural Numbers = "<<sum;
   cout<<endl;
   return 0;
}

Output

C++ Program to Find the Sum of First N Natural Numbers