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

sum of n natural numbers

Sum of N natural number

In this C++ program we will find the sum of n natural numbers. First, the user have to insert any integer value.  Then user is asked to enter a value of n and run for loop to the range of n. Inside for loop for every cycle value will be incremented by i.
N natural numbers means 1,2,3,4,5,6,7,8…….infinite
To perform the arithmetic addition operation of n numbers where n will be entered by the user.

For Example:-
Enter number : 8
Where first 8 number is 1,2,3,4,5,6,7,8
Sum of numbers = 1+2+3+4+5+6+7+8 = 36

Competitive Coding Techniques

Algorithm:-

Step 1 : Ask the user to enter a  number which will denote the limit.

Step 2 : Declare a variable to store the addition and initialize it with 0.

Step 3 : Use a loop to perform iterations for adding natural numbers from 1 till the limit entered by the user.

Step 4 : Use a statement for addition purpose.

Step 5 : Print the result.

Find the sum of N natural numbers

C++ Code:-

#include<iostream>
using namespace std;
int main() 
{ 
    //for initialize variable
    int Number, i, Sum = 0;
    //to take user input
    cout<<"\n Kindly Insert an Integer Variable\n"; cin>>Number;

    //use for loop for these condition
    for(i = 1; i <= Number; i++)
    {
       Sum = Sum + i;
    }

    //display
    cout<<"Sum of Natural Numbers: "<< Sum;

    return 0;
}

Output

Kindly Insert an Integer Variable
4
Sum of Natural Numbers: 10