C#: Add given number of months between zero and fifteen months to a given date
Write a C# Sharp program to add a 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
{
// Main method, entry point of the program
public static void Main()
{
// Create a DateTime object 'dat' set to August 31, 2016
var dat = new DateTime(2016, 8, 31);
// Loop through 15 iterations to add months to 'dat' and display the result
for (int ctr = 0; ctr <= 15; ctr++)
{
// Add 'ctr' months to the initial date 'dat' and display the date in short format (d)
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:
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?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics