-3
List<int> number= new List<int> { 0, 5, 6, 9, 14, 21, 45, 76, 123, 172, 212, 313 };

output: even numbers: 0, 6, 14, 76, 172, 212 odd numbers: 5, 9, 21, 123, 313

like this with a foreach and an if / else

What I tried:

 static void Main(string[] args)
        {
            List<int> number= new List<int> { 0, 5, 6, 9, 14, 21, 45, 76, 123, 172, 212, 313 };
            foreach (int numbers in number)
            {

                if (numbers % 2 == 0)
                {
                    Console.WriteLine(numbers);
                }
                else
                {
                    Console.WriteLine(numbers);
                }

            }
            Console.WriteLine("even number:"numbers);
            Console.WriteLine("odd number:"+numbers);
            Console.ReadLine();
        }
Lucas Mue
  • 9
  • 4
  • 1
    `number.Where(p => (p & 1) == 1)` for all the odd numbers, `number.Where(p => (p & 1) == 0)` for all the even numbers – Prime Mar 26 '21 at 07:47
  • 1
    number%2==0 is even ,others are odd – auslander Mar 26 '21 at 07:47
  • 1
    You could show us the code you have tried. – Kristoffer Jälén Mar 26 '21 at 07:48
  • You put all the numbers into the exact same list. Use two lists instead, one for the odds and one for the evens. – MakePeaceGreatAgain Mar 26 '21 at 07:55
  • 1
    Does this answer your question? [Testing if a list of integer is odd or even](https://stackoverflow.com/questions/18818680/testing-if-a-list-of-integer-is-odd-or-even) – Drag and Drop Mar 26 '21 at 08:03
  • And [How to output a List of numbers in odd and even?](https://stackoverflow.com/questions/13079368/). [Select even/odd elements in IEnumerable?](https://stackoverflow.com/questions/11269847/) – Drag and Drop Mar 26 '21 at 08:04
  • https://stackoverflow.com/questions/2366634/c-sharp-load-integers-and-display-odd-evenn, – Drag and Drop Mar 26 '21 at 08:06
  • `var g = numbers.ToLookup(x => x % 2 == 0);` even: `g[true]`), odd: `g[false]` – Magnus Mar 26 '21 at 08:07
  • Ok here is one with toloopuk https://stackoverflow.com/questions/29206334/split-string-into-two-array-on-behalf-of-odd-or-even , https://stackoverflow.com/questions/24402368/how-can-i-group-odd-and-even-using-linq, https://stackoverflow.com/questions/267033/getting-odd-even-part-of-a-sequence-with-linq https://stackoverflow.com/questions/11938877/does-linq-natively-support-splitting-a-collection-in-two https://stackoverflow.com/questions/24402368/how-can-i-group-odd-and-even-using-linq, in fact name a C" top user i can find an asnwer of him on a dupe – Drag and Drop Mar 26 '21 at 08:15
  • @HimBromBeere I need to do it like this – Lucas Mue Mar 26 '21 at 08:23

2 Answers2

0

You can achieve it with the method below

Odd Numbers

List<int> oddNumbers = number.Where(x => x % 2 != 0).ToList();

Even Numbers

List<int> evenNumbers = number.Where(x => x % 2 == 0).ToList();

** Update: As Post owner update question and request for print the result, so here is the solution

Console.WriteLine("Odd numbers: " +String.Join(',', oddNumbers));
Console.WriteLine("Even numbers: " +String.Join(',', evenNumbers));
Yong Shun
  • 35,286
  • 4
  • 24
  • 46
0

You´re just printing - or at least trying to do so - the entire list on every iteration. Actually you want to print the current number. Furterhmore you should devide your input-list into two lists, one for the even numbers and one for the odd numbers. Last you can print those lists by iterating their elements:

var even = new List<int>; 
var odd = new List<int>();
foreach (int numbers in number)
{
    if (numbers % 2 == 0)
    {
        even.Add(number);
    }
    else
    {
        odd.Add(number);
    }
}

This can be even simplified by a single linq:

var odd = numbers.Where(x => x % 2 == 1).ToList();
var even = numbers.Where(x => x % 2 == 0).ToList();

In order to print those lists, you´d need this:

Console.WriteLine("Odd Numbers: " + string.Join(',', odd));
Console.WriteLine("Even Numbers: " + string.Join(',', even));

If you´d just use Console.WriteLine(odd) you´d get List{1}, because there´s no generic implementation for how to print a list (ToString will just return the type which is List<T>).

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • Thank you it helped me a lot, I didn´t got the idea that I just could create new lists! – Lucas Mue Mar 26 '21 at 08:39
  • 1
    Side note: it's possible to get rid of `if` and put one liner `foreach (int numbers in number) ((numbers % 2 == 0) ? even : odd).Add(number);` – Dmitry Bychenko Mar 26 '21 at 08:58