Question:
Write recursive method that for a positive integer n prints odd numbers

Answer:

I usually use binary check to solve this type of questions:

if ((N & 1) != 0) then N is odd

So the code would look like


for(int i=0;i<=N;i++)
{
if ((i & 1) != 0)
{
Console.WriteLine(i);
}
}


Another, simplier option is to use increment by 2


for(int i=1;i<=N;i+=2)
{
Console.WriteLine(i);
}






Answers and Comments


Saved Stories

Sponsored Categories