Answer the question
In order to leave comments, you need to log in
How to write faster?
int summ = 0;
foreach (int r in new int[] { 5, -6, 2, 7, -5, 9, 1, -3 }) summ += (r < 0) ? r : 0;
Answer the question
In order to leave comments, you need to log in
Maybe so? Outline for readability
int summ = 0;
int[] massive = new int[] {5, -6, 2, 7, -5, 9, 1, -3};
foreach (int r in massive)
{
if (r < 0)
{
summ += r;
}
}
int summ = (new int[] {5, -6, 2, 7, -5, 9, 1, -3}).Where(x => x < 0).Sum();
int result = new int[] { 5, -6, 2, 7, -5, 9, 1, -3 }.Where(i => i<0).Sum();
foreach (int r in new int[] { -6, -5, -3 }) summ += r;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question