C# Sharp Exercises: Date of past and future fifteen years of a specified date
C# Sharp DateTime: Exercise-15 with Solution
Write a C# Sharp program to display the date of past and future fifteen years of a specified date.
Sample Solution:-
C# Sharp Code:
using System;
public class Example15
{
public static void Main()
{
DateTime baseDate = new DateTime(2016, 2, 29);
Console.WriteLine(" Base Date: {0:d}\n", baseDate);
// Show dates of previous fifteen years.
for (int ctr = -1; ctr >= -15; ctr--)
Console.WriteLine("{0,2} year(s) ago: {1:d}",
Math.Abs(ctr), baseDate.AddYears(ctr));
Console.WriteLine();
// Show dates of next fifteen years.
for (int ctr = 1; ctr <= 15; ctr++)
Console.WriteLine("{0,2} year(s) from now: {1:d}",
ctr, baseDate.AddYears(ctr));
}
}
Sample Output:
Base Date: 2/29/2016 1 year(s) ago: 2/28/2015 2 year(s) ago: 2/28/2014 3 year(s) ago: 2/28/2013 4 year(s) ago: 2/29/2012 5 year(s) ago: 2/28/2011 6 year(s) ago: 2/28/2010 7 year(s) ago: 2/28/2009 8 year(s) ago: 2/29/2008 9 year(s) ago: 2/28/2007 10 year(s) ago: 2/28/2006 11 year(s) ago: 2/28/2005 12 year(s) ago: 2/29/2004 13 year(s) ago: 2/28/2003 14 year(s) ago: 2/28/2002 15 year(s) ago: 2/28/2001 1 year(s) from now: 2/28/2017 2 year(s) from now: 2/28/2018 3 year(s) from now: 2/28/2019 4 year(s) from now: 2/29/2020 5 year(s) from now: 2/28/2021 6 year(s) from now: 2/28/2022 7 year(s) from now: 2/28/2023 8 year(s) from now: 2/29/2024 9 year(s) from now: 2/28/2025 10 year(s) from now: 2/28/2026 11 year(s) from now: 2/28/2027 12 year(s) from now: 2/29/2028 13 year(s) from now: 2/28/2029 14 year(s) from now: 2/28/2030 15 year(s) from now: 2/28/2031
Flowchart:

C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a C# Sharp program to add specified number of months between zero and fifteen months to the last day of August, 2016.
Next: Write a C# Sharp program that compares two dates.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Exercises: Weekly Top 12 Most Popular Topics
- Pandas DataFrame: Exercises, Practice, Solution
- Conversion Tools
- JavaScript: HTML Form Validation
- SQL Exercises, Practice, Solution - SUBQUERIES
- C Programming Exercises, Practice, Solution : For Loop
- Python Exercises, Practice, Solution
- Python Data Type: List - Exercises, Practice, Solution
- C++ Basic: Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - exercises on Employee Database
- SQL Exercises, Practice, Solution - exercises on Movie Database
- SQL Exercises, Practice, Solution - exercises on Soccer Database
- C Programming Exercises, Practice, Solution : Recursion