Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 632669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:06:06+00:00 2026-05-13T20:06:06+00:00

Hello wondering if there is an easier way to display odd / even numbers.

  • 0

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.

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:06:07+00:00Added an answer on May 13, 2026 at 8:06 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering if there is a way around this (or if it is even
Hello I was wondering if there is a nice way to find out what
Hello I am wondering if there is a way to change a string value
Hello Python experts, I am wondering if there is any shorter way to assign
Hello i am wondering if there is a way to check if a certain
Hello fellow Grails Developers! I was wondering if you could help me with what
I'm wondering if there is a way to call variable functions with namespaces. Basically
Hello fellow Computer People! I could do this myself, but was just wondering if
I'm writing a custom tunnel (first there's a custom hello and then the connection
hello i am new to phpexcel, and i was wondering if there is some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.