Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
How to Print alternate Even And Odd Number using Linq?
Posted
Updated 20-Jan-16 0:59am
v2
Comments
F-ES Sitecore 20-Jan-16 7:01am    
What do you mean by alternate even and odd? Give an example of your expected output.
BillWoodruff 20-Jan-16 7:13am    
If we do your homework, you will learn nothing.
vinod 23 20-Jan-16 23:44pm    
Means I want to Print first alternate Even Number And then Alternate odd number.

Well you would start by analysing the problem, breaking it down into simple steps and then turning those steps into code. Much like the advice you have already received at How to print alternate prime number upto given range ?[^].
 
Share this answer
 
How about
C#
Console.WriteLine(string.Join("\n", Enumerable.Range(0, 1000)));
Is this "LINQ'ish" enough? ;-)
Cheers
Andi
PS: Or if you want only the even numbers, you may add a Where(n => (n & 1) == 0) to the Range(...) call.
 
Share this answer
 
v4
This may help..
C#
class Program
    {
        static void Main(string[] args)
        {
            List<int> listint = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
            var listodd = listint.Where(x => x % 2 != 0).ToList();
            var listeven = listint.Where(x => x % 2 == 0).ToList();

            Console.WriteLine("---Printing odd numbers----\n");
            string cssodd = string.Join(",", listodd);
            Console.Write(cssodd);
            
            Console.WriteLine("\n\n---Printing even numbers----\n");
            string csseven = string.Join(",", listeven);
            Console.Write(csseven);
            Console.ReadKey();

        }
    }

Thanks
 
Share this answer
 
v2
Comments
Richard MacCutchan 20-Jan-16 7:44am    
You do not help people by doing their work for them.
Andreas Gieriet 20-Jan-16 7:51am    
Hello Richard,
it's a bit of a dilemma. Answering such simple things may help opening the blocking knot. In the end, they have to be able to explain the solution to their teacher - and finally pass the exams, I guess. Giving a bit of a hint in what direction it might go may serve as some kind of a kick-start ;-).
Cheers
Andi
[no name] 20-Jan-16 7:53am    
+Andreas Gieriet agreed!
Richard MacCutchan 20-Jan-16 7:57am    
Andi, I have no problem with giving hints, but when you provide a complete solution to a "how do I ... " question, the questioner is less likely to make an effort to learn for themself.
[no name] 20-Jan-16 8:04am    
Richard MacCutchan Okay sir ! i will take care of that while answering..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900