Testing if a list of integer is odd or even

后端 未结 5 497
借酒劲吻你
借酒劲吻你 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);
    

提交回复
热议问题