Testing if a list of integer is odd or even

后端 未结 5 480
借酒劲吻你
借酒劲吻你 2020-12-29 20:13

Trying to determine if my list of integer is made of odd or even numbers, my desired output is a list of true an/or false. Can I perform the following operation on the list

相关标签:
5条回答
  • 2020-12-29 20:51

    Just use the modulus

    loop through the list and run the following on each item

    if(num % 2 == 0)
    {
      //is even
    }
    else
    {
      //is odd
    }
    

    Alternatively if you want to know if all are even you can do something like this:

    bool allAreEven = lst.All(x => x % 2 == 0);
    
    0 讨论(0)
  • 2020-12-29 21:04
            #region even and odd numbers
            for (int x = 0; x <= 50; x = x + 2)
            {
    
                int y = 1;
                y = y + x;
                if (y < 50)
                {
                    Console.WriteLine("Odd number is #{" + x + "} : even number is #{" + y + "} order by Asc");
                    Console.ReadKey();
                }
                else
                {
                    Console.WriteLine("Odd number is #{" + x + "} : even number is #{0} order by Asc");
                    Console.ReadKey();
                }
    
            }
    
            //order by desc
    
            for (int z = 50; z >= 0; z = z - 2)
            {
                int w = z;
                w = w - 1;
                if (w > 0)
                {
                    Console.WriteLine("odd number is {" + z + "} : even number is {" + w + "} order by desc");
                    Console.ReadKey();
                }
                else
                {
                    Console.WriteLine("odd number is {" + z + "} : even number is {0} order by desc");
                    Console.ReadKey();
                }
            }
    
    0 讨论(0)
  • 2020-12-29 21:07

    There's at least 7 different ways to test if a number is odd or even. But, if you read through these benchmarks, you'll find that as TGH mentioned above, the modulus operation is the fastest:

    if (x % 2 == 0)
                   //even number
            else
                   //odd number
    

    Here are a few other methods (from the website) :

    //bitwise operation
    if ((x & 1) == 0)
           //even number
    else
          //odd number
    
    //bit shifting
    if (((x >> 1) << 1) == x)
           //even number
    else
           //odd number
    
    //using native library
    System.Math.DivRem((long)x, (long)2, out outvalue);
    if ( outvalue == 0)
           //even number
    else
           //odd number
    
    0 讨论(0)
  • 2020-12-29 21:14

    --simple codes--

            #region odd / even numbers  order by desc
    
            //declaration of integer
            int TotalCount = 50;
            int loop;
    
            Console.WriteLine("\n---------Odd Numbers -------\n");
    
            for (loop = TotalCount; loop >= 0; loop--)
            {
                if (loop % 2 == 0)
                {
                    Console.WriteLine("Even numbers : #{0}", loop);
                }
            }
    
            Console.WriteLine("\n---------Even Numbers -------\n");
    
            for (loop = TotalCount; loop >= 0; loop--)
            {
                if (loop % 2 != 0)
                {
                    Console.WriteLine("odd numbers : #{0}", loop);
                }
            }
    
            Console.ReadLine();
    
            #endregion
    
    0 讨论(0)
  • 2020-12-29 21:18

    You could try using Linq to project the list:

    var output = lst.Select(x => x % 2 == 0).ToList();
    

    This will return a new list of bools such that {1, 2, 3, 4, 5} will map to {false, true, false, true, false}.

    0 讨论(0)
提交回复
热议问题