
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate Sum of Natural Numbers in C++
In this article, we will write a C++ program to calculate the sum of the first n natural numbers. Natural numbers are positive numbers starting from 1(i.e., 1, 2, 3, ...). We will take a positive number n as input and find the sum of all natural numbers from 1 up to n.
Let's understand with examples:
Input: n = 5 The sum of the first 5 natural numbers is: 1 + 2 + 3 + 4 + 5 = 15 Output: 15 Input: n = 8 The sum of the first 8 natural numbers is: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36 Output: 36
Approaches for Calculating Sum of Natural Numbers
We can solve this problem in two ways:
Using a Loop
In this approach, we use a loop to add each natural number one by one until we reach the given number. It's a simple direct way to solve the problem by manually performing the addition inside the program.
Example
Here's a complete C++ program, where we take a natural number n, initialize a sum variable, and use a loop to add each number from 1 to n.
#include <iostream> using namespace std; int main() { int n = 5; int sum = 0; // loop for calculating sum manually for (int i = 1; i <= n; ++i) { sum += i; } cout << "The total of first "<<n<< " natural numbers is: " << sum << endl; return 0; }
The output of the above program shows the sum of the first n natural numbers.
The total of first 5 natural numbers is: 15
Time Complexity: O(n), because we are running a loop n times.
Space Complexity: O(1).
Using a mathematical formula
In this approach, we use the formula sum = n * (n + 1) / 2 to directly calculate the sum of the first natural numbers. It's simple and fast because we don't use any loop or extra steps.
Example
Here's a C++ program that implements the above approach to calculate the sum.
#include <iostream> using namespace std; int main() { int n = 5; // appplying formula int sum = n * (n + 1) / 2; cout << "Summation result of first "<<n<< " natural numbers: " << sum << endl; return 0; }
Below is the output for the sum of first 5 natural numbers:
Summation result of first 5 natural numbers: 15
Time Complexity: O(1), because only one calculation is needed.
Space Complexity: O(1).
Using Recursion
In this approach, we use a recursive function that keeps calling itself by decreasing the value of n by 1 until it reaches 1. At each step, the function adds the current number to the result of the recursive call with the previous value.
Example
Here's a C++ program that uses recursion to calculate the sum of the first n natural numbers.
#include <iostream> using namespace std; // Recursive function to calculate sum of first n natural numbers int sumNatural(int n) { if (n == 1) // base case: when n is 1, return 1 return 1; return n + sumNatural(n - 1); // recursive call } int main() { int n = 5; int sum = sumNatural(n); // print the result cout << "Recursive sum of first " << n << " natural numbers: " << sum << endl; return 0; }
Below shows the output of the above program, where the sum of the first 5 natural numbers is calculated using recursion.
Recursive sum of first 5 natural numbers: 15
Time Complexity:O(n) because the function makes one call for each number from n to 1
Space Complexity:: O(n) because each recursive call adds a new frame to the call stack.