Introduction:
Arrays are an essential data structure in programming, allowing us to store and manipulate collections of elements. In this blog post, we will dive into the world of array manipulation in C# and focus on calculating the sum of even numbers within an array. We will explore multiple methods to achieve this goal, providing step-by-step explanations along with code examples. By the end of this article, you will have a solid understanding of various approaches to summing even numbers in C# arrays.
Method 1: Iterative Approach
The first method we'll explore is the iterative approach. This method involves iterating through each element in the array, checking if it is an even number, and adding it to a running sum. Here's an example implementation in C#:
int[] numbers = { 2, 5, 8, 10, 15, 16, 20 };
int sum = 0;
foreach (int num in numbers)
{
if (num % 2 == 0)
{
sum += num;
}
}
Console.WriteLine("Sum of even numbers: " + sum);
Output:
Sum of even numbers: 56
In this method, we start by initializing the sum variable to 0. Then, we iterate through each element in the array using a foreach loop. Within the loop, we use the modulus operator (%) to check if the current number is divisible by 2 without leaving a remainder, which indicates that it is an even number. If the condition is met, we add the number to the sum. Finally, we display the sum of even numbers using the Console.WriteLine()
method.
Method 2: LINQ Query
C# provides the Language-Integrated Query (LINQ) feature, which allows us to perform complex queries on collections of data. We can leverage LINQ to simplify the process of summing even numbers in an array. Here's an example implementation:
int[] numbers = { 2, 5, 8, 10, 15, 16, 20 };
int sum = numbers.Where(num => num % 2 == 0).Sum();
Console.WriteLine("Sum of even numbers: " + sum);
Output:
Sum of even numbers: 56
In this approach, we use the Where()
method from the System.Linq
namespace to filter the array and retrieve only the even numbers. The lambda expression num => num % 2 == 0
serves as the filter condition. After filtering the array, we apply the Sum()
method to calculate the sum of the filtered even numbers. Finally, we display the sum using Console.WriteLine()
.
Method 3: User-Defined Method
Additionally, you can create a user-defined method that encapsulates the logic for summing even numbers. This approach provides reusability and enhances code organization. Here's an example:
static int SumEvenNumbers(int[] numbers)
{
int sum = 0;
foreach (int num in numbers)
{
if (num % 2 == 0)
{
sum += num;
}
}
return sum;
}
// Usage
int[] numbers = { 2, 5, 8, 10, 15, 16, 20 };
int sum = SumEvenNumbers(numbers);
Console.WriteLine("Sum of even numbers: " + sum);
Output:
Sum of even numbers: 56
In this method, we define a static method named SumEvenNumbers
that takes an array of integers as a parameter. The logic for summing even numbers is similar to the iterative approach discussed earlier. We iterate through each element in the array, check if it is even, and add it to the sum. The method then returns the calculated sum.
Conclusion:
In this blog post, we explored various methods to calculate the sum of even numbers within an array in C#. We started with an iterative approach, which involved iterating through the array and checking each element. We then introduced LINQ as a powerful tool for querying and manipulating arrays, allowing us to simplify the code using a LINQ query. Finally, we discussed the option of creating a user-defined method for reusability.
Comments (0)