w3resource logo
C# Sharp Programming Exercises

C# Sharp Exercises: Add given number of months between zero and fifteen months to a given date

C# Sharp DateTime: Exercise-14 with Solution

Write a C# Sharp program to add specified number of months between zero and fifteen months to the last day of August, 2016.

Sample Solution:-

C# Sharp Code:

using System;
public class Example14
{
   public static void Main()
   {
      var dat = new DateTime(2016, 8, 31);
      for (int ctr = 0; ctr <= 15; ctr++)
         Console.WriteLine(dat.AddMonths(ctr).ToString("d"));

   }
}

Sample Output:

8/31/2016                                                                                                     
9/30/2016                                                                                                     
10/31/2016                                                                                                    
11/30/2016                                                                                                    
12/31/2016                                                                                                    
1/31/2017                                                                                                     
2/28/2017                                                                                                     
3/31/2017                                                                                                     
4/30/2017                                                                                                     
5/31/2017                                                                                                     
6/30/2017                                                                                                     
7/31/2017                                                                                                     
8/31/2017                                                                                                     
9/30/2017                                                                                                     
10/31/2017                                                                                                    
11/30/2017

C# Sharp Code Editor:

using System;
public class Example14
{
   public static void Main()
   {
      var dat = new DateTime(2016, 8, 31);
      for (int ctr = 0; ctr <= 15; ctr++)
         Console.WriteLine(dat.AddMonths(ctr).ToString("d"));

   }
}

Improve this sample solution and post your code through Disqus