How to check if a number is Even or Odd in C#?

  

3
Argomento iniziale

I need to create a program which checks if a number (random one) is Even or Odd in Visual Studio. And prints the result

4 risposte
2

Check this code out:

using System;
 
class RandomNumberCheck
{
    static void Main()
    {
        Random rnd = new Random();
        int a = rnd.Next(0, 100);
        Console.WriteLine(a);
 
        if (a % 2==0)
        {
            Console.WriteLine("Even");
        }
        else
        {
            Console.WriteLine("Odd");
        }
    }
}
1

Another solution:

using System;
 
class OddOrEvenIntegers
{
    static void Main()
    {
        Console.WriteLine("Please write your number, so the expression can check whether it is odd or even:");
        int n = int.Parse(Console.ReadLine());
 
        bool result = n % 2 == 0;
        Console.WriteLine("Your number {0} is even? Answer: {1}", n, result);
    }
}
1
using System;
 
    class EvenOrOdd
    {
        static void Main()
        {
            Console.WriteLine("Enter an integer number: ");
            int number = int.Parse(Console.ReadLine());
            bool check = number % 2 != 0;
            Console.WriteLine("Does the number is odd?");
            Console.WriteLine(check);
        }
    }
1

Here is my solution with bool:

using System;
 
class Program
{
    static void Main()
    {
        int n = int.Parse(Console.ReadLine());
        bool result = (n % 2 != 0 && n > 0);
 
        Console.WriteLine("Odd? {0}", result);
    }
}
Condividi: