Answered by:
Why the output is 2,4,6 please explain
Question

int [] i = new int[] {1,2,3,4,5,6}; foreach (int num in i) { if (num%2==0) { Console.WriteLine(num); } }
I am beginner .
why the output is = 2,4,6
please explain ...
Tuesday, September 26, 2017 10:45 AM
Answers

Hi santosh,
You could look into the following tutorial about how modulus operation works.
Modulus only returns value that is only divide by "x" (num%x), in your case 2 and remainder will be zero only to output those.
Example:
2÷2=1 and remainder is 0
4÷2=2 and remainder is 0
6÷2=3 and remainder is 0
So, you get output 2,4,6
Thanks,
Sabah Shariq[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]
 Edited by Sabah ShariqMVP Tuesday, September 26, 2017 11:26 AM
 Marked as answer by JonPaker Tuesday, September 26, 2017 11:33 AM
Tuesday, September 26, 2017 11:18 AM 
int [] i = new int[] {1,2,3,4,5,6}; foreach (int num in i) { if (num%2==0) { Console.WriteLine(num); } }
.
why the output is = 2,4,6
quotient as the division operator / does.
For the expression x % 2 any value of x which is evenly divisible by 2 will
return a remainder of zero.
 Wayne
Tuesday, September 26, 2017 10:55 AM
All replies

int [] i = new int[] {1,2,3,4,5,6}; foreach (int num in i) { if (num%2==0) { Console.WriteLine(num); } }
.
why the output is = 2,4,6
quotient as the division operator / does.
For the expression x % 2 any value of x which is evenly divisible by 2 will
return a remainder of zero.
 Wayne
Tuesday, September 26, 2017 10:55 AM 
Hi santosh,
You could look into the following tutorial about how modulus operation works.
Modulus only returns value that is only divide by "x" (num%x), in your case 2 and remainder will be zero only to output those.
Example:
2÷2=1 and remainder is 0
4÷2=2 and remainder is 0
6÷2=3 and remainder is 0
So, you get output 2,4,6
Thanks,
Sabah Shariq[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]
 Edited by Sabah ShariqMVP Tuesday, September 26, 2017 11:26 AM
 Marked as answer by JonPaker Tuesday, September 26, 2017 11:33 AM
Tuesday, September 26, 2017 11:18 AM