CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2012
    Posts
    12

    even odd numbers using foreach

    Hello Friends,

    How can we generate even odd series using foreach loop.
    so far my code is:

    public void forEachLoop()
    {
    Console.WriteLine("Enter Starting point:");
    first = int.Parse(Console.ReadLine());
    Console.WriteLine("Enter Ending point:");
    last = int.Parse(Console.ReadLine());
    int[] array = new int[last];
    foreach (int num in array)
    {
    array[first] = first;
    array[last] = last;
    array[first] += 2;
    Console.WriteLine(array[first]);


    if (array[first] == array[last])
    break;
    }

    }

    Please help,Thank you.

  2. #2
    Join Date
    Jan 2007
    Posts
    491

    Re: even odd numbers using foreach

    Code:
    //print the even numbers between 0 and 100
    for(int i=0;i<=50;i++)
       Console.WriteLine(2*i);
    Or:
    Code:
    //print the odd numbers between 0 and 100
    foreach(int i in Enumerable.Range(0, 50))
       Console.WriteLine(2*i+1);

  3. #3
    Join Date
    Mar 2012
    Posts
    12

    Re: even odd numbers using foreach

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured