HomeC++C++ Program to Calculate the Sum of Natural Numbers

C++ Program to Calculate the Sum of Natural Numbers

In this post, you will learn how to Calculate the Sum of Natural Numbers using C++.

This lesson will teach you how to calculate the sum of natural numbers, with a mathematical function using the C++ Language. Let’s look at the below source code.

How to Calculate the Sum of Natural Numbers?

RUN CODE SNIPPET

Source Code

#include<iostream>
using namespace std;
int main() 
{ 
   int n, sum;
   cin>> n;
   cout<<" Enter a value : "<< n << endl;
   sum = n*(n+1)/2;
   cout<<"\n Sum of first "<< n <<" natural numbers is : "<<sum;
   return 0; 
}

Input

5

Output

Enter a value : 5
Sum of first 5 natural numbers is : 15

#include <iostream>

  • This line which is called the header file. #include statement tells the compiler to use available files and <iostream>is the name of the specific that we have used in this code. The <iostream> file stands for Input and Output statement.

using namespace std;

  • The C++ has a standard library that has files for different functions and this line is used to access the standard file for input and output statements.

int main();

  • This line usually controls the function of the code, as it calls the functions to perform their tasks.
  • The int main()shows that the input value is a type of integer, once the program is executed the function returns to the main function, by using the statement ‘return 0;’.

{ }

  • The opening ‘ { ‘ and the closing ‘ } ‘ curly braces mark the start and the finish of the main function
  • Every statement and value between these braces belong to the main function.

The above statements are the main factors that support the function of the source code. Now we can look into the working and layout of the code’s function.

To find the sum of natural numbers we use the following mathematical formula

C++ Program to find the Sum of Natural Numbers
  1. First we declare the variable n and sum  as integers. We then collect the number from the user and store it in n using function cin>> ,display the value using cout<< and the Insertion Operators'<<‘ , ‘>>’.
  2. Using the formula sum = n*(n+1)/2and the value n, we calculate the sum of natural numbers and store it in the integer  sum.
  3. Using the output statement cout display the answer.
  4. End the function with return 0; this returns the function to main( ).

Note: The ‘ << endl ‘ in the code is used to end the current line and move to the next line and ‘\n’ is also a new line function, to understand how both the functions work exclude it from the code, move it around and work with it.

Additional Source Code

You can also try the source code given below, the following code is structured using for loop and a mathematical function. Let’s look at the below source code.

RUN CODE SNIPPET
#include<iostream>
using namespace std;
int main()
{
  int n, i, sum=0;
  cin >> n;
  cout << "Enter the n value: "<< n << endl;
  for (i = 1; i <= n; i++)
  {
    sum += i; 
  }
 cout<<"\n Sum of first "<< n <<" natural numbers is : "<<sum;
  return 0;
}

Input

5

Output

Enter the n value: 5
Sum of first 5 natural numbers is : 15
  1. In this source code after declaring the integers we will be using and getting the value form the user, we then use a for loop. A for loop is used in C++ language to repeat a function continuously until the condition is satisfied. The formula to find the sum of the natural numbers is written in the form of a for loop. The syntax of the for loop is
    for (variable initialization; condition; increment operation) { //loop statements; }
  2. We can work on the for loop in the above source code to understand better
    for (i = 1; i <= n; i++) { sum += i; }
  3. In the for loop first is the initialization of the variablei = 1next the condition to be satisfied i <= n which is followed by increment operator i++ .
  4. The next line in the for loop is the loop statement sum += i which is executed after the for loop is run.
  5. The loop statement is expanded as sum = sum + i where the initial value of sum is 0 and i is 1. Now when the for loop is executed, if the condition is satisfied, i.e  i is less than or equal to n, the value i is in incremented by one and the mathematical loop statement is executed.
  6. Now the value of sum changes as per the loop statement function. The execution moves back up to the for loop and is executed repeatedly until the value i is not less than or equal to n. Now the function terminates and the last sum value is the Sum of n Natural Numbers.
  7. The rest of the statements are similar to find the Sum of Natural Numbers.

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this post, you will learn how to Generate Multiplication Table using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Display Fibonacci Sequence using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Find GCD of two Numbers using C++ programming language. This lesson will...
  • C++
  • January 30, 2022