How to check if a number is odd or even without modulus operator in C#

1 Answer

0 votes
using System;

class Program
{
    static bool is_even(int n) { 
        return ((n / 2) * 2 == n); 
    } 
    static void Main()
    {
        int n = 13;

        string s = is_even(n) ? "Even" : "Odd";  
        
        Console.WriteLine(s);
    }
}


/*
run:

Odd

*/

 



answered Mar 27, 2019 by avibootz

Related questions

...