Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to Find the Sum of First N Natural Numbers
We are given a number N, and we need to calculate the sum of the first N natural numbers. Natural numbers are positive integers starting from 1 (i.e., 1, 2, 3, 4, 5, ...). In this article, we are going to learn how we can find the sum of the first N natural numbers in C#.
Example 1
Input
N = 3
Output
6
Explanation
The first 3 natural numbers are: 1, 2, 3
The sum of these numbers is: 1 + 2 + 3 = 6
Example 2
Input
N = 5
Output
15
Explanation
The first 5 natural numbers are: 1, 2, 3, 4, 5
The sum of these numbers is: 1 + 2 + 3 + 4 + 5 = 15
Using Iterative Approach
This is a simple and direct approach to find the sum. We use a loop to calculate the sum of each number from 1 to N and add it to a cumulative sum variable.
Algorithm
- Step 1 Initialize a variable sum to 0.
- Step 2 Loop through each number from 1 to N.
- Step 3 For each number, add it to the sum.
- Step 4 Return the final value of the sum.
Example
using System;
class Program {
static int SumOfNaturalNumbersIterative(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
return sum;
}
static void Main() {
int N = 3;
int result = SumOfNaturalNumbersIterative(N);
Console.WriteLine("The sum of the first {0} natural numbers is: {1}", N, result);
N = 10;
result = SumOfNaturalNumbersIterative(N);
Console.WriteLine("The sum of the first {0} natural numbers is: {1}", N, result);
}
}
The output of the above code is
The sum of the first 3 natural numbers is: 6 The sum of the first 10 natural numbers is: 55
Time Complexity: O(N) We iterate through N numbers once.
Space Complexity: O(1) We use constant extra space.
Using Formula-Based Approach
In this method, we use a mathematical formula to find the sum without iterating through numbers. The formula for the sum of the first N natural numbers is: Sum = N × (N + 1) / 2. This formula is derived from the arithmetic progression series.
Algorithm
- Step 1 Create a function.
- Step 2 Inside the function, use the formula to calculate the sum of the first N natural numbers.
- Step 3 Return the calculated sum.
Example
using System;
class Program {
static int SumOfNaturalNumbersFormula(int n) {
int sum = n * (n + 1) / 2;
return sum;
}
static void Main() {
int N = 5;
int result = SumOfNaturalNumbersFormula(N);
Console.WriteLine("The sum of the first {0} natural numbers is: {1}", N, result);
N = 100;
result = SumOfNaturalNumbersFormula(N);
Console.WriteLine("The sum of the first {0} natural numbers is: {1}", N, result);
}
}
The output of the above code is
The sum of the first 5 natural numbers is: 15 The sum of the first 100 natural numbers is: 5050
Time Complexity: O(1) Formula calculation takes constant time.
Space Complexity: O(1) We use constant extra space.
Using Recursive Approach
In this approach, we use recursion to find the sum of the first N natural numbers. For each recursive call, we add the current number to the result of the remaining numbers.
Algorithm
- Step 1 Define a recursive function SumOfNaturalNumbersRecursive.
- Step 2 Define a base case: If n = 0, return 0.
- Step 3 For the recursive case, return n + SumOfNaturalNumbersRecursive(n - 1).
Example
using System;
class Program {
static int SumOfNaturalNumbersRecursive(int n) {
if (n == 0) return 0;
return n + SumOfNaturalNumbersRecursive(n - 1);
}
static void Main() {
int N = 4;
int result = SumOfNaturalNumbersRecursive(N);
Console.WriteLine("The sum of the first {0} natural numbers is: {1}", N, result);
N = 6;
result = SumOfNaturalNumbersRecursive(N);
Console.WriteLine("The sum of the first {0} natural numbers is: {1}", N, result);
}
}
The output of the above code is
The sum of the first 4 natural numbers is: 10 The sum of the first 6 natural numbers is: 21
Time Complexity: O(N) We make N recursive calls.
Space Complexity: O(N) Each recursive call adds a frame to the call stack.
Comparison of Approaches
| Approach | Time Complexity | Space Complexity | Best Use Case |
|---|---|---|---|
| Iterative | O(N) | O(1) | Easy to understand, memory efficient |
| Formula-based | O(1) | O(1) | Fastest execution, large values of N |
| Recursive | O(N) | O(N) | Educational purposes, functional programming |
Real-Life Applications
- Mathematical Operations: The calculation of sums of numbers is fundamental in many mathematical problems and operations.
- Scientific Analysis: It is used in physics, chemistry, and other sciences for deriving formulas and solving equations that involve natural numbers.
- Data Analysis: This type of calculation helps in statistical models, budgeting, and financial forecasting where sums of series are required.
- Algorithm Analysis: Understanding time complexity often involves calculating sums of natural numbers.
Conclusion
Finding the sum of first N natural numbers can be achieved through iterative, formula-based, or recursive approaches. The formula-based approach (N × (N + 1) / 2) is the most efficient with O(1) time complexity, making it ideal for large values of N and performance-critical applications.
