|
-
April 8th, 2012, 07:47 AM
#1
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.
-
April 8th, 2012, 08:03 AM
#2
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);
-
April 8th, 2012, 08:19 AM
#3
Re: even odd numbers using foreach
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|