For the entered day, your code will display the beginning, end, and number (from 1 to 4) of the year's quarter that the day belongs to.
You need to transform the month number into the quarter's number like this:
You can use integer division to get the result you need.
You need to add 2 to the month number first and perform integer division by 3 after that.
int numberOfQuarter = (enteredMonth + 2) / 3;
If you have the quarter's number, you get the quarter's first month like this:
The quarter's number has to be multiplied by 3.
To get the correct results, subtract 2 subsequently.
int monthOfQuarterStart = 3 * numberOfQuarter - 2;
To get the first day, you use the DateTime constructor with the day number set to 1.
To get the last day, you add three months and subtract one day.
using System; class Program{//from ww w . ja v a 2 s . com static void Main(string[] args){ // Date input Console.Write("Enter a date: "); string input = Console.ReadLine(); DateTime enteredDate = Convert.ToDateTime(input); // Calculations int enteredYear = enteredDate.Year; int enteredMonth = enteredDate.Month; int numberOfQuarter = (enteredMonth + 2) / 3; int monthOfQuarterStart = 3 * numberOfQuarter - 2; DateTime firstDayOfQuarter = new DateTime(enteredYear, monthOfQuarterStart, 1); DateTime lastDayOfQuarter = firstDayOfQuarter.AddMonths(3).AddDays(-1); // Outputs Console.WriteLine(); Console.WriteLine("Corresponding quarter: " + "number-" + numberOfQuarter + ", from " + firstDayOfQuarter.ToShortDateString() + " to " + lastDayOfQuarter.ToShortDateString()); } }