Sinisterly
[C#] Even number - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C)
+--- Thread: [C#] Even number (/Thread-C-Even-number)



[C#] Even number - H3RR3S - 07-26-2019

Hello,
When you want to see if a numer is even in C# we use the code:

Code:
( x % 2 == 0)

But why is that? I don't see any logic explanation behind it?


RE: [C#] Even number - OversouL - 07-26-2019

EDIT: Wrong explanation.

Actually modulo gives you the remainder when you divide. The result of an odd number when halved is 1 and while even numbers are 0.


RE: [C#] Even number - darkninja1980 - 07-26-2019

Is this for making a program in C?


RE: [C#] Even number - Spore - 07-26-2019

(07-26-2019, 06:45 AM)darkninja1980 Wrote: Is this for making a program in C?
C#, its in the title
[Image: SOs4uYN.png]


RE: [C#] Even number - darkninja1980 - 07-26-2019

(07-26-2019, 07:09 AM)Spore Wrote:
(07-26-2019, 06:45 AM)darkninja1980 Wrote: Is this for making a program in C?
C#, its in the title
[Image: SOs4uYN.png]
What I mean is he can show the whole source code.


RE: [C#] Even number - Tracefl0w - 07-26-2019

Code:
x % 2
gives the remainder after the integer division (when dealing with only integers such as in this case, otherwise a common type) of x/2. The % is called the modulo operator. Of course when the remainder is 0, the number is even.
Source


RE: [C#] Even number - Yotic - 06-30-2021

IF x divisible by 2 , The remainder = 0, true


RE: [C#] Even number - sunjester - 08-09-2021

this is in C

Code:
#include <stdio.h> int main() {     int n = 4;         if(n%2==0)     {         printf("n is even");     } else {         printf("n is odd");     }         return 0; }

random test case
Code:
#include <stdio.h> #include <stdlib.h> int main() { int i, n; time_t t; srand((unsigned) time(&t)); for(i=0;i<9;i++) { n = rand()%50; if(n%2==0) { printf("%d is even\n", n); } else { printf("%d is odd\n", n); } } return 0; }


24 is even
20 is even
25 is odd
43 is odd
33 is odd
16 is even
1 is odd
44 is even
7 is odd


RE: [C#] Even number - sunjester - 11-28-2021

and here is the same modulus operator (%) and using a ternary to replace the if/else. It's a nice way to refactor lines and to get alternating row colors, turning something on and off, or even counting binary bits.

Code:
/// sunjester 2021 /// OddEvenTernary.cs using System; namespace OddEvenTernary {     class OddEvenTernary     {                static void Main(string[] args)         {             String oddEven = String.Empty;             for(int i=0;i<=10;i++)             {                 oddEven = (i%2==1 ? "Odd" : "Even");                 Console.WriteLine(i + " is " + oddEven);             }         }     } }

Code:
c:\software>csc OddEvenTernary.cs && OddEvenTernary.exe Microsoft (R) Visual C# Compiler version 3.11.0-4.21403.6 (ae1fff34) Copyright (C) Microsoft Corporation. All rights reserved. 0 is Even 1 is Odd 2 is Even 3 is Odd 4 is Even 5 is Odd 6 is Even 7 is Odd 8 is Even 9 is Odd 10 is Even