Check odd and even number inside for loop - CSharp Language Basics

CSharp examples for Language Basics:for

Description

Check odd and even number inside for loop

Demo Code

using System;//w w  w  .  j  a  v a2 s  .  c  o m
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
   static void Main(string[] args)
   {
      // Output
      for (int number = 1; number <= 20; number++)
      {
         Console.Write(number);
         // What goes after the number depends on even/odd test
         if (number % 2 != 0)
         {
            // Odd number, displaying space
            Console.Write(" ");
         }
         else
         {
            // Even number, new line
            Console.WriteLine();
         }
      }
   }
}

Result


Related Tutorials