C++ Program to find the Sum of N Natural Numbers

Program to find the Sum of N Natural Numbers

Here we will discuss how to find the Sum of N Natural Numbers in c++ programming language, N being the limit which user will define.

All the positive integers (1, 2, 3, 4…) are the Natural Numbers.

Formula To find the sum of first N Natural Numbers 

            Sum = N(N+1)/2

 

C++ Program to find the Sum of N Natural Numbers

Algorithm:-

  1. Take input n, natural number upto which we have to find sum
  2. calculate sum using formula for sum of first n natural numbers
    1. sum= n*(n+1)/2
  3. print sum

C++ Code:-

    
    //C++ Program
    // Sum of n natural numbers 
    #include<iostream>
    using namespace std;
    int main()
    {
        
       int sum , N; 
        cout << “Enter the limit: “
        //user input 
        cin >> N; 
        //calculating 
        sum sum= N*(N+1)/2
        cout<<“The Sum of first “<< N <<” Natural Numbers is “<< sum
        return 0;
    }
Output:-

Enter the limit: 20
The Sum of first 20 Natural Numbers is 210