How to sum up an array of integers in C#
Provided that you can use .NET 3.5 (or newer) and LINQ, try
int sum = arr.Sum();
sum up an array of integers in C#
It isn't clear what you want... If you want to sum some numbers so that the sum stops summing when it reaches 1002 you can:
List<int> list = new List<int> { 4, 900, 500, 498, 4 };
var lst = list.Skip(2).Take(3);
int sum = 0;
foreach (int num in lst)
{
int sum2 = sum + num;
if (sum2 > 1002)
{
break;
}
sum = sum2;
}
Console.WriteLine(sum);
or you can
List<int> list = new List<int> { 4, 900, 500, 498, 4 };
var lst = list.Skip(2).Take(3);
int sum = 0;
foreach (int num in lst)
{
sum += num;
if (sum >= 1002)
{
break;
}
}
Console.WriteLine(sum);
The first one will stop if by adding a number it would go over 1002, while the second will stop AFTER reaching 1002.
Note that both of these can't be easily done directly in LINQ.
Sum of selected values in an int Array in c#
If I understand it correct and using your code this is what you want.
int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int sum = 1 + 2;
int total=0;
foreach (int n in numbers)
{
if (n % 3 == 0 || n % 5 == 0)
{
int total += n;
}
else
{
Console.WriteLine("not divisible");
}
}
Console.WriteLine(total);
I moved the printing out to after the foreach so you get one result when it is done
Array.Sum() results in an overflow
The issue is that while the individual values fit within an int
, the sum of these numbers results is larger than an int
can hold.
You therefore need to cast the values to long
(or another datatype that takes numbers that big, but since you're using Int64...):
long sum = arr.Sum(v => (long)v);
Array of total even numbers
You are checking the parity of the loop index i
. Use nums[i]
in your for
loop instead:
if (nums[i] % 2 == 0)
// ... rest of code
Is it possible to sum all integers in a list? (C# console)
The other answer recommends LINQ, and that is definitely the less verbose way to do it. But I have a feeling that you're just learning, so may want to have an idea of how this actually works. Below is the an abridged version of the Linq.Sum
source code so you can see what is happening.
public static int Sum(this IEnumerable<int> source)
{
int sum = 0;
foreach (int v in source)
{
sum += v;
}
return sum;
}
It loops through each item in the IEnumerable, adds them to another variable, and then returns that result.
Related Topics
Differencebetween a Property and a Variable
How to Read Data from a Zip File Without Having to Unzip the Entire File
Failed to Serialize the Response in Web API
What Is an MVChtmlstring and When Should I Use It
How to Check If Tcpclient Connection Is Closed
Is There Anything Like Asynchronous Blockingcollection<T>
How to Add Claims in ASP.NET Identity
How to Add an Extra Button to the Window's Title Bar
One to One Optional Relationship Using Entity Framework Fluent API
How to Copy Part of an Array to Another Array in C#
How to Distinguish Between Multiple Input Devices in C#
Default Value of a Type at Runtime
Leave Only Two Decimal Places After the Dot
How to Replace All the Spaces with %20 in C#