C++ Program to Calculate Sum of Natural Numbers

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

Natural numbers include numbers from 1 to infinity and are a part of the real number system. Natural numbers are also called counting numbers and it is important to note that zero is not included in natural numbers. Programming can be used to compute the sum of natural numbers either by using the sum of natural numbers formula or other programming practices.

Introduction to Sum of Natural Numbers Formula

The difference between two consecutive natural numbers is 1. Since every two consecutive numbers have the same difference, the sequence of natural numbers is an arithmetic progression.

The sequence of an arithmetic progression is, $a, a+d, a+2d, a+3d,...$ where a is the first element of the number and d is the common difference between two consequent numbers of the sequence.

The sum of n numbers of an arithmetic progression can be calculated using the following formula,

$S_n = {\frac{n*(2a+(n-1)d)}{2}}$

where,

  • a is the first term of the series
  • d is the common difference between two consecutive terms of the series.
  • n is the number of terms of the series

The above formula can be further simplified as, Substituting a=1 and d=1 in the above equation, we can attain the equation of the sum of natural numbers, $n*{\frac{(n+1)}{2}}$

The n in the equation depicts the number of elements in the sequence.

Program to Calculate the Sum of Natural Numbers Using Formula

The formula discussed in the above section can be implemented using the C++ programming language.

Example

The code to find the sum of natural numbers using the sum of natural numbers formula is as follows,

The brackets are used in the formula around (natural_number+1) because of the precedence of the operator in C++. In C++, a multiplication or division operator has a higher priority than addition or subtracter operator which means that multiplication and division will be performed before addition. To prevent this we use a bracket that has higher precedence than other operators.

Output The output for the above example is,

Explanation

The number of elements in the sequence is illustrated with the variable natural_number in the code. This variable is received as input from the user. The sum is calculated by substituting the number of elements in the sequence in the sum of natural numbers formula. The output is displayed to the user.

The time complexity of the program is O(1) or constant time complexity. The space complexity of the program is also O(1).

Program to Calculate the Sum of Natural Numbers Using For Loop

A for loop can be used to iterate the number in the natural number sequence and add them to find the sum of natural numbers.

Example

The for loop can be implemented in two ways. The following program shows a for loop which iterates from 1 to the natural number,

The following program shows a for loop which iterates from the natural number to 1(starting number of the sequence of the natural numbers),

Output

Explanation

The maximum number in the natural number sequence is received from the user. A for loop is made to iterate from 1 to the maximum natural number or from the maximum natural number to 1. The sum is added at each iteration to get the total sum of natural numbers.

The time complexity of both the programs will be O(n) where n represents the number of iterations by the for loop. The space complexity of both programs will be O(1).

Program to Calculate the Sum of Natural Numbers Using Do While Loop

A do..while loop runs for the first time without checking for the conditions provided to the loop and only checks for conditions from the second iteration of the loop.

Example

Output

Explanation

The natural number up to which the sum must be calculated is received as input. The do..while loop adds all numbers from one to the number entered as input. The result is displayed to the user.

The time complexity of the program is O(n) where n is the number of iterations made in the do...while loop. The space complexity of the program is O(1).

Program to Calculate the Sum of Natural Numbers Using Recursion

Recursion is a technique in which a function is called repetitively within the same function. Recursion can be used instead of loops to find the sum of natural numbers.

Example

Output

Explanation

The natural number is received as input. A recursion function call is made which calls the same function with the parameter as the one subtracted by the number passed as an argument to the last function call. The recursion call will exit only if the number becomes 0. The output returned by the function is added at the end of each function call to get the sum of natural numbers.

The time complexity of the sum of natural numbers using recursion is O(n). The space complexity of a recursive function depends on the number of function calls made. In the case of the above example, the space complexity is O(n).

The order of call of functions will be,

After Sum(0) is reached, the function returns zero and the result of each function is accumulated backward to find the sum,

The accumulation is performed in the same step as the return function. If the function is Sum(0), then the value returned is 0 and if the function is Sum(1), the value returned will be 0+1 where the 0 is returned by the Sum(0) function. Similar accumulations are performed at each consecutive function to get the sum of n natural numbers.

Explore Scaler Topics C++ Tutorial and enhance your C++ skills with Reading Tracks and Challenges.

Conclusion

  • Natural numbers start from one and are also called counting numbers.
  • The sum of natural numbers can be found using the sum of natural numbers formula which is derived from arithmetic progression as the natural number sequence follows an arithmetic progression.
  • The sum of natural numbers can also be found by iterative approaches using for, while, or do..while loops.
  • Sum of natural numbers can also be found using a recurrence function.

See Also: