C++ Program to Find the sum of First N natural Number

In this article, we will see C++ Program to Find the Sum of First N Natural Numbers. A Natural number is the same as a Counting number. They are used to Count the number of real physical objects. Natural numbers start from 1 and go on infinite.

The positive numbers 1,2,3 ... are known as natural numbers.

The formula for the Sum of First n natural numbers is n(n+1)/2.

If you want to add the first 5 Natural numbers then we find the Sum of 1+2+3+4+5 =15.

Flow Diagram:-

sum1.jpg

Code:-

  • Start.

  • Declare and initialize variable, i,num, s=0.

  • Enter the value of num i.e. number up to which sum is to be calculated.

  • Use Loop to add numbers. Any of while, do-while or for loop can be used.

  • Print the sum.

  • Stop.

Algorithm:-

Code:-

#include<bits/stdc++.h> 
using namespace std;

int main()
{
    int n;
    cout << "Enter a number : "; 
    cin >> n;
    
    int sum=0;
    
    for(int i=1;i<=n;i++) 
       {

                sum+=i;

        }

    cout << sum;

    return 0;
}

Output:-

  5

  Sum is 15