简体   繁体   中英

C# Load integers and display odd / even

Hello wondering if there is an easier way to display odd / even numbers. I know I could do a for loop and load a list. Then I can write another for loop to loop through the list and check if a value is odd / even:

for(i=0; i<100; i++)
 if(myList[i]%2==0) //even
    //do something
 else
    //odd do something

But is there any way to shorten this up just so that I can easily get a list of odd or even numbers. Not homework just wondering.

The LINQ way... Odd and Even numbers between 1 and 100.

var even = Enumerable.Range(1,100).Where(i => i % 2 == 0);
var odd = Enumerable.Range(1,100).Where(i => i % 2 != 0);

Could you use some sort of lambdas:

//load a list, t, with 100 integers
List<int> t = Enumerable.Range(1, 100).ToList();

//find odd numbers
var oddNumbers = t.Where(num => num%2 != 0);

//find even numbers
var evenNumbers = t.Where(num => num%2 == 0);

//print odd numbers
foreach (int i in oddNumbers)
    Console.WriteLine(i);

//print even numbers
    foreach(int i in evenNumbers)
        Console.WriteLine(i);

The Enumerable just loads the list with 1-100, and then I simply snatch all odds / evens and then print them. This all can be shortened to:

var e = Enumerable.Range(1, 100).Where(num => num%2==0); //for even numbers
var o = Enumerable.Range(1, 100).Where(num => num%2!=0); //for odd numbers

e,o have an implicit type var. The compiler can determine its type so these two lines are equivalent to:

List<int> eo = Enumerable.Range(1, 100).ToList(); //must tell it its a list

Then to find the odds / evens directly to a list type:

List<int> o = eo.Where(num => num%2!=0).ToList();
List<int> e = eo.Where(num => num%2==0).ToList();

And to print it is listed in my initial code.

You can use LINQ to pull out just the odd or even, and then process:

var even = myList.Where(i => i%2==0);
foreach(var number in even)
     // do something
var t = Enumerable.Range(1, 100).ToList();
var oddNumbers = t.Where(n => (n & 1) != 0).ToList();
var evenNumbers = t.Where(n => (n & 1) == 0).ToList();

One mistake here: you need to include int before i=1; so that i becomes a recognised integer.

for (int i = 1; i < 101; i++)
{
   if (i % 2 != 0)
   {
      //do something
   }
}

Or even:

for (int i = 1, i < 101, i+=2)
{
   //do something
}

Populate your list according to these formulas

Odds[0->N] = 2*i+1
Evens[0->N] = 2*i

If you only need half of the numbers, just generate half the numbers ( .. ; .. ; i = i + 2)

If you need all the numbers but want to avoid an additional loop, you can mark them or process them during the initial loop.

Or, during creation, make up to three lists/arrays - one for all numbers, another for odds, and the third for evens.

Is there a specific application?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM