C# Program to Find Sum of N Natural Numbers
You can find the sum of n natural numbers in C# using
int sum = n * (n + 1) / 2; or by looping from 1 to n and adding each number.Examples
Input1
OutputSum of first 1 natural numbers is 1
Input5
OutputSum of first 5 natural numbers is 15
Input10
OutputSum of first 10 natural numbers is 55
How to Think About It
To find the sum of the first n natural numbers, think of adding all numbers from 1 up to n. You can do this by adding each number one by one or use the formula
n * (n + 1) / 2 which gives the total directly.Algorithm
1
Get the input number n from the user2
Calculate the sum using the formula n * (n + 1) / 23
Print the resultCode
csharp
using System; class Program { static void Main() { Console.Write("Enter a number: "); int n = int.Parse(Console.ReadLine() ?? "0"); int sum = n * (n + 1) / 2; Console.WriteLine($"Sum of first {n} natural numbers is {sum}"); } }
Output
Enter a number: 5
Sum of first 5 natural numbers is 15
Dry Run
Let's trace the input 5 through the code
1
Input
User enters 5, so n = 5
2
Calculate sum
sum = 5 * (5 + 1) / 2 = 5 * 6 / 2 = 30 / 2 = 15
3
Output
Prints 'Sum of first 5 natural numbers is 15'
| n | sum |
|---|---|
| 5 | 15 |
Why This Works
Step 1: Input reading
The program reads the number n from the user using Console.ReadLine() and converts it to an integer.
Step 2: Sum calculation
It uses the formula n * (n + 1) / 2 which adds all natural numbers from 1 to n efficiently without looping.
Step 3: Output display
The program prints the sum with a clear message using string interpolation $"...".
Alternative Approaches
Using a for loop
csharp
using System; class Program { static void Main() { Console.Write("Enter a number: "); int n = int.Parse(Console.ReadLine() ?? "0"); int sum = 0; for (int i = 1; i <= n; i++) { sum += i; } Console.WriteLine($"Sum of first {n} natural numbers is {sum}"); } }
This method uses a loop to add each number one by one, which is easy to understand but slower for very large n.
Using recursion
csharp
using System; class Program { static int SumNatural(int n) { if (n == 1) return 1; return n + SumNatural(n - 1); } static void Main() { Console.Write("Enter a number: "); int n = int.Parse(Console.ReadLine() ?? "0"); int sum = SumNatural(n); Console.WriteLine($"Sum of first {n} natural numbers is {sum}"); } }
This method uses recursion to add numbers, which is elegant but can cause stack overflow for large n.
Complexity: O(1) time, O(1) space
Time Complexity
Using the formula, the calculation is done in constant time without loops.
Space Complexity
Only a few variables are used, so space is constant.
Which Approach is Fastest?
The formula approach is fastest; looping or recursion take O(n) time and more memory.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Formula | O(1) | O(1) | Fastest calculation for any n |
| For loop | O(n) | O(1) | Simple logic, easy to understand |
| Recursion | O(n) | O(n) | Elegant but risky for large n |
Use the formula n*(n+1)/2 for the fastest and simplest sum calculation.
Forgetting to convert the input string to an integer before calculation.