Sure, here is a C# program to find the sum of all even numbers in an array of integers:
C#
using System;
public class SumOfEvenNumbers {
public static int Sum(int[] array) {
// Initialize the sum to 0
int sum = 0;
// Iterate through the array
for (int i = 0; i < array.Length; i++) {
// Check if the current number is even
if (array[i] % 2 == 0) {
// Add the current number to the sum
sum += array[i];
}
}
// Return the sum
return sum;
}
public static void Main() {
// Create an array of integers
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Find the sum of all even numbers in the array
int sum = Sum(array);
// Print the sum
Console.WriteLine("The sum of all even numbers in the array is: " + sum);
}
}
This program will first create an array of integers. It will then call the Sum method to find the sum of all even numbers in the array. The program will then print the sum.
Here is an example of how to run the program:
Code snippet
C#> SumOfEvenNumbers.exe
The sum of all even numbers in the array is: 30
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is a C# program to find the sum of all even numbers in an array of integers:
C#
This program will first create an array of integers. It will then call the Sum method to find the sum of all even numbers in the array. The program will then print the sum.
Here is an example of how to run the program:
Code snippet