Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


[C#] Even number filter_list
Author
Message
[C#] Even number #1
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?
As long as you are living, you will face problems. But you serve a God who solves problems.

[+] 1 user Likes H3RR3S's post
Reply

RE: [C#] Even number #2
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.
(This post was last modified: 07-26-2019, 01:34 AM by OversouL.)
[Image: dHJ4Beo.gif]
Hidden Lesson: Reactions are always instinctive whereas responses are always well thought of.

Reply

RE: [C#] Even number #3
Is this for making a program in C?
My IT skills that I know perfect is SQL, HTML ,css ,wordpress, PHP.
coding skills that I know is Java, JavaScript and C#

Reply

RE: [C#] Even number #4
(07-26-2019, 06:45 AM)darkninja1980 Wrote: Is this for making a program in C?
C#, its in the title
[Image: SOs4uYN.png]
(This post was last modified: 07-26-2019, 07:10 AM by Spore.)
COM1 Serial User.

Reply

RE: [C#] Even number #5
(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.
My IT skills that I know perfect is SQL, HTML ,css ,wordpress, PHP.
coding skills that I know is Java, JavaScript and C#

Reply

RE: [C#] Even number #6
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


Ransomware is more about manipulating vulnerabilities in human psychology than the adversary’s technological sophistication.

Reply

RE: [C#] Even number #7
IF x divisible by 2 , The remainder = 0, true

Reply

RE: [C#] Even number #8
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
(This post was last modified: 08-09-2021, 08:47 PM by sunjester.)

Reply

RE: [C#] Even number #9
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

Reply







Users browsing this thread: 2 Guest(s)