C# programs to find the sum of the series $1 + 2 + 3 + ... + n$ i.e. the sum of the first $n$ natural numbers have been shown here. Both the iterative and recursive approaches have been shown below. The sum can also be computed using the mathematical formula $\frac{n(n+1)}{2}$.
Example:
Suppose $n = 7$
So the sum of first $7$ natural numbers = $1 + 2 + 3 + 4 + 5 + 6 + 7 = 28$
By using the mathematical formula, we obtain the sum of first $7$ natural numbers = $\frac{7 \cdot (7+1)}{2} = 28 $
The algorithm, pseudocode and time-complexity of the program have also been covered in the following sections.
Page content(s):
1. Algorithm to find sum of the series 1 + 2 + 3 + ... + n / Sum of the First N Natural Numbers
1. Take number of terms $n$ as input.
2. Set $sum = 0$.
3. Add each $i \in [1, n]$, to $sum$.
4. Display $sum$ as output.
2. Pseudocode to find sum of the series 1 + 2 + 3 + ... + n / Sum of the First N Natural Numbers
Input: Number of terms $n$
Output: Sum of $1+2+3+...+n$
1. Procedure sumSeries($n$):
2.
3.
4.
5.
6. End Procedure
3. Time complexity to find sum of the series 1 + 2 + 3 + ... + n / Sum of the First N Natural Numbers
Time Complexity: O(n)
Here $n$ is the number of terms.
Time Complexity (Using formula): O(1)
4. Program & output to find the sum of the series 1 + 2 + 3 + ... + n / Sum of the First N Natural Numbers
4.1. C# Program & output to find sum of the series 1 + 2 + 3 + ... + n / Sum of the First N Natural Numbers using formula
/****************************************** alphabetacoder.com C# Program to find sum of series 1+2+3+... + n using the formula n(n + 1) / 2 *******************************************/ using System; namespace SumofSeries { class Program { static void Main(string[] args) { // delclare variables int n, sum; // take input Console.Write("Enter no of terms: "); n = Convert.ToInt32(Console.ReadLine()); // calculate the sum using formula sum = (n * (n + 1)) / 2; // display result Console.WriteLine("Sum: " + sum); // wait for user to press any key Console.ReadKey(); } } }
Output
Enter no of terms: 10
Sum: 55
4.2. C# Program to find sum of the series 1 + 2 + 3 + ... + n / Sum of the First N Natural Numbers using iteration
/********************************* alphabetacoder.com C# Program to find sum of series 1+2+3+...+ n using iteration **********************************/ using System; namespace SumofSeries { class Program { static void Main(string[] args) { // delclare variables int n, sum = 0, i; // take input Console.Write("Enter no of terms: "); n = Convert.ToInt32(Console.ReadLine()); // calculate the sum iteration for (i = 1; i <= n; i++) sum += i; // display result Console.WriteLine("Sum: " + sum); // wait for user to press any key Console.ReadKey(); } } }
Output
Enter no of terms: 150
Sum: 11325
4.3. C# Program to find sum of the series 1 + 2 + 3 + ... + n / Sum of the First N Natural Numbers using recursion
/********************************* alphabetacoder.com C# Program to find sum of series 1+2+3+...+ n using recursion **********************************/ using System; namespace SumofSeries { class Program { // recursive function to calculate sum // of series 1 + 2 + 3 ... + n int calculate_sum(int n) { // exit condition of recursive call if (n == 0) return 0; // call function return n + calculate_sum(n - 1); } static void Main(string[] args) { // delclare variables int n; // declare object Program obj = new Program(); // take input Console.Write("Enter no of terms: "); n = Convert.ToInt32(Console.ReadLine()); // display result by calling the // recursive function Console.WriteLine("Sum: " + obj.calculate_sum(n)); // wait for user to press any key Console.ReadKey(); } } }
Output
Enter no of terms: 5
Sum = 15