0

Why Output is 2,4,6 please explain

var numbers = new int[] {1, 2, 3, 4,5,6}; foreach (var number in numbers) { if (numbers%2==0) { Console.WriteLine(number); } }

26th Sep 2017, 5:40 AM
santosh kumar
santosh kumar - avatar
2 Answers
+ 5
% is modulus. That what is left over after a division. So if you do n%2==0 and the result is 0. The number is even.
26th Sep 2017, 6:01 AM
sneeze
sneeze - avatar
0
This code outputs multiples of 2 in the range [1,6]
26th Sep 2017, 6:47 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar