int[] numbers = {5, -6, 2, 7, -5, 9, 1, -3}; int summ = 1; for (int i = 0; i <= numbers.Length; i++) { if (i < 0) { summ += numbers[i]; } } Console.WriteLine(summ); Console.ReadKey(); 

answer gives 1 don't understand what to do

Closed due to the fact that off-topic participants Dmitriy Simushev , zRrr , rdorn , aleksandr barakin , user194374 8 Jun '16 at 14:46 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - Dmitriy Simushev, zRrr, rdorn, aleksandr barakin, Community Spirit
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Comments are not intended for extended discussion; conversation moved to chat . - Nick Volynkin

4 answers 4

And I want to write the answer))

 int summ=0; foreach(int r in new int[]{ 5, -6, 2, 7, -5, 9, 1, -3 } ) summ+=(r<0)?r:0; 
  • Difficult language yes? simple, I did not understand anything but most importantly everything works, thanks - Zeroone
  • @Zeroone array wrapped in foreach, could foreach (int r in numbers) summ + = / * sum * / (r <0 / * condition * /)? R / * if yes * /: 0 / * if not * / ; - nick_n_a
  • thanks for the clarification - Zeroone
  • @nick_n_a © I would not answer questions-tasks similar to labs. Please do not turn the forum into the trash. - Paulo Berezini

Here you can not do without Linq.
Where function to select negative
Sum function to get the amount

 int[] numbers = { 5, -6, 2, 7, -5, 9, 1, -3 }; var r = numbers.Where(el=>el<0).Sum(); 

You can also use Sum overload and do without Where

 int[] numbers = { 5, -6, 2, 7, -5, 9, 1, -3 }; var r = numbers.Sum(el=>el<0?el:0); 
  • Well, you bent it) You can always do without LINQ) - DreamChild
  • one
    Teaching a student for such an answer))))) - nick_n_a
  • Well, what Linq? A person cannot clearly write a for loop, so let's stick it in Linq. - Igor
  • @Igor, but why should he for if everything is already invented before us? :-) - Grundy
  • 3
    @AlexKrass, it means they have a bad university :) - Grundy
 int summ = 0; //здесь была первая ошибка for(int i = 0; i < numbers.Length; i++) //здесь была третья ошибка найденная Igor { if (numbers[i]<0) //здесь была вторая ошибка { summ += numbers[i]; } } 
  • one
    i <= numbers.Length error # 3 - Igor
  • @Igor, yes you are right, I looked through the third, but already corrected - Mirdin
  • I think so correctly, thanks for the answer, but now it catches the indexoutofrangeexception operation; probably this is another question already - Zeroone
  • @Zeroone, there was a third error in the comparison - remove the '=' symbol - Mirdin 1:58 pm
  • @Mirdin thanks, earned - Zeroone
 sum = numbers.Where(w=>w < 0).Sum(); 

Even easier :)

  • The third answer is the same as this one - nick_n_a
  • @nick_n_a, I have a parameter in el lambda and here w :-) - Grundy
  • Tooechly soryan, I somehow watch all the cycles offered, ternary operations. Linq is easier - Vladimir Paliukhovich
  • Minus for duplicate.stackoverflow.com/a/530006/182771 - Dmitriy Simushev