make for loop and find sum in csharp

Here's an example of using a for loop to find the sum of an array of integers in C#:

main.cs
int[] numbers = { 1, 2, 3, 4, 5 };
int sum = 0;

for (int i = 0; i < numbers.Length; i++)
{
    sum += numbers[i];
}

Console.WriteLine("The sum of the numbers is: " + sum);
174 chars
10 lines

In this example, we create an integer array called numbers with five elements. We also create an integer variable called sum and initialize it to 0.

Then, we use a for loop to iterate through the numbers array. For each element, we add its value to the sum variable.

Finally, we print out the total sum using the Console.WriteLine method.

This code will output: The sum of the numbers is: 15

related categories

gistlibby LogSnag