Find Sum of Array in C#
What you’ll learn
- The accumulator pattern with
sum += value. - Fixed-array and user-input variants in C#.
- Handling empty arrays and negative values safely.
- Why complexity is linear in the number of elements.
Overview
Array sum means adding every element exactly once. For {1, 2, 3, 4, 5}, the result is 15. This is the core accumulator pattern used in many interview problems.
Two C# programs
Fixed array sum and user-input array sum.
Live preview
Paste values like 10, -2, 7 and compute instantly.
Safe sum type
Use long when totals might exceed int.
Prerequisites
int[] arrays, foreach or for loops, and basic console I/O.
- You know how to declare and initialize an array like
int[] array = {1, 2, 3};. - You can read input with
Console.ReadLine()and parse withint.TryParse.
Understanding the concept of sum of array
The sum of an array is obtained by adding up all the elements it contains — each value is counted exactly once.
For {1, 2, 3, 4, 5}, the sum is 1 + 2 + 3 + 4 + 5 = 15.
Accumulator pattern
Start with sum = 0, then for each element x do sum = sum + x. After the loop, sum holds the total.
If there are no elements, the loop never runs and the sum stays 0.
Live preview
Enter integers separated by commas or spaces (for example: 1 2 3 or 1,2,3).
Algorithm
Goal: compute the sum of all elements in an array.
Initialize sum
Start with sum = 0 (or long sum = 0).
Traverse array
Visit each element once in a foreach or for loop.
Accumulate
Add each value into the running sum and return it.
📜 Pseudocode
function arraySum(arr):
sum = 0
for each value in arr:
sum = sum + value
return sum Sum of a fixed array
Reference program: sums {1, 2, 3, 4, 5} using a foreach loop.
using System;
class Program
{
static long FindSumOfArray(int[] arr)
{
long sum = 0;
foreach (int element in arr)
sum += element;
return sum;
}
static void Main()
{
int[] array = { 1, 2, 3, 4, 5 };
long sum = FindSumOfArray(array);
Console.WriteLine($"Sum of the array elements: {sum}");
}
} Sum of user-input array
Reads n values from the console and accumulates the total — good practice for interview I/O.
using System;
class Program
{
static void Main()
{
Console.Write("Enter number of elements: ");
if (!int.TryParse(Console.ReadLine(), out int n) || n < 0)
{
Console.WriteLine("Invalid n.");
return;
}
long sum = 0;
for (int i = 0; i < n; i++)
{
Console.Write($"Enter element {i + 1}: ");
if (!int.TryParse(Console.ReadLine(), out int x))
{
Console.WriteLine("Invalid element.");
return;
}
sum += x;
}
Console.WriteLine($"Sum of the array elements: {sum}");
}
} Optimization and alternatives
One pass is optimal. A single traversal is already O(n) for an exact sum.
Use long. Safer totals when many large values are added.
LINQ (optional). array.Sum() works in .NET, but interviews often expect an explicit loop.
❓ FAQ
🔄 Input / output examples
| Array | Sum |
|---|---|
{1, 2, 3, 4, 5} | 15 |
{10, -2, 7} | 15 |
{} (empty) | 0 |
Edge cases and pitfalls
Summing is straightforward, but these cases are commonly asked in interviews.
Zero elements
No elements means the total stays 0.
Large totals
Use long to reduce overflow risk when numbers are large.
Validate parsing
Use int.TryParse and reject invalid tokens before summing.
⏱️ Time and space complexity
| Approach | Time | Extra space |
|---|---|---|
| Single traversal sum | O(n) | O(1) |
Summary
- Pattern: initialize
sum = 0, then add each value once. - Type: use
longfor safer totals. - Complexity:
O(n)time andO(1)extra space.
Summing an array is the simplest form of the accumulator pattern: start with 0 and keep adding each element.
9 people found this page helpful
