C++ program to calculate sum of the first n natural numbers

C++ program to calculate sum of the first n natural numbers
A natural number is a number that occurs commonly and obviously in nature. As such, it is a whole, non-negative number. The set of natural numbers, denoted N, can be defined in either of two ways:

N = {0, 1, 2, 3, ...}
N = (1, 2, 3, 4, ...}


For the better understanding of this program you have to know more about the following C++ programming topic(s) :
C++ Flow Control

Sum of the first n natural numbers can be calculated using the for loop or the formula.

The formula is to calculate sum of the first n natural numbers is

sum = n(n+1)/2

 

The for loop is to calculate sum of the first n natural numbers is

For loop is run from 1 to n. In each iteration of  the loop, the value of i is added to the sum. So, the sum of the first n natural numbers is obtained.
1+2+3+4+5.....+n

Example: formula is to calculate sum of the first n natural numbers


Output

Enter a positive integer: 10
Sum of first 10 natural numbers is 55
Press Enter to return to Quincy...


Example: for loop is to calculate sum of natural numbers


Output

Enter a positive integer: 10
Sum of first 10 natural numbers is 55
Press Enter to return to Quincy...



Post a Comment

Thank you for vising

Previous Post Next Post