# CSharp - Write program to get start, end, and number of the year's quarter a day belongs to.

## Requirements

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.

## Hint

You need to transform the month number into the quarter's number like this:

• Month 1, 2, or 3 = 1
• Month 4, 5, or 6 = 2
• Month 7, 8, or 9 = 3
• Month 10, 11, or 12 = 4

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:

• 1 (January) for the first quarter
• 4 (April) for the second quarter
• 7 (July) for the third quarter
• 10 (October) for the fourth quarter

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.

## Demo

using System;
class Program{//from  w  w  w  . j a va  2 s . c  o  m
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());
}
}