w3resource
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

Flowchart:

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

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write C# Sharp Program to add 30 seconds and the number of seconds in one day to a DateTime value.
Next: Write a C# Sharp program to display the date of past and future fifteen years of a specified date.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming