C++ Program to find sum of n Natural numbers.

In this article, we will be writing a C++ Program to find sum of n Natural numbers. We will first understand the concept behind it and the logic which we gonna use to write this program then we will continue writing the C++ program.

It is one of the important program in C++ generally for the beginners to understand the basic functioning of the C ++ concepts. Where we’re going to take a number from the user and calculate sum till that number. Before writing this C++ program you must be aware what natural numbers are , below our short explanation on Natural numbers :-

Natural numbers are the numbers which includes all the positive integers from 1 till infinity it does not include 0 (zero) .

Example of Natural numbers – (1 , 2 , 3 , 4 , 5 ,6 ,7 ,8…….)

1. Program outline

In this program first we take input from the user and store it in a variable in C++ program. we use cin to take input from the user. After taking input from the user we run our for loop till that inputted number by incrementing it by 1 and adding each time we increment our number. This all comes in our Calculation part , later we print the concluded output .

2. Concept

The basic understanding you should have before writing this C++ Program to find sum of n Natural numbers are :-

Logic behind this program

The main logic behind writing this program is :-

  • first we take input from the
    user upto which we have to print sum of natural numbers then we store it in a variable n.
  • For printing sum of n natural numbers we need to Start for loop from 1 to inputted number and increment 1 while each iteration.
  • It will look like for( i=1 , i <= n , i++ )
  • Then inside for loop we print our value using variable sum.

Its good if you understood and if not you’ll understand while writing its code . Lets head towards writing its C++ program.

3. Program

C++ program of the above question.

// Simple C++ program to display "C++ Program to find sum of n Natural numbers." 
  
// Header file for input output functions 
#include<iostream>  
  
using namespace std; 
  
 
int main() 
{ 
    int n;
    int sum = 0;
   cout<<"Enter a positive integer: ";
   cin>>n;
   for(int i=1;i<=n;i++)
   {
     sum=sum+i;
   }
      
      cout<<"Sum till inputted number is: "<<sum<<endl;
      return 0; 
} 

4. Output

we finally reached our conclusion after writing C++ program , let’s check what output it gives .

Sum of n natural numbers.

If you find something incorrect or have any questions or feedback please comment down below Thank you.

Similar Posts

Leave a Reply

Your email address will not be published.