RUN


Approaching the problem:

While working with dates we have to keep in mind a variety of cases as months have different number of days. Below is a list of possible cases we have to take care of:

# When day=28 and it’s a February
In this we would have to check if it’s a leap year or not and then set the next date accordingly.

# Month ends of various months
For January, March, May, July, August, October, and December last day is 31. For February it is 28 or 29 depending on it’s a leap year or not. And for rest, it’s 30. So we need to check a combination of month and day before incrementing the month.

# Last day of the year
If its 31st December i.e. last day of the year then the month will be set to 1 and date to 1 and year will be incremented by 1.

Also while printing the date we would have to check if the day and month to be printed are less than 10 as then they will be followed by a zero.

For leap year we will follow the conditions of Georgian Calendar, which states a year is a leap year if:
– It is divisible by 400
– It is divisible by 4 and not divisible by 100

Algorithm:

  1. Since the date can be inputted in a variety of formats, like 1 Dec 2020 or 1/12/2020 or 1/12/20 or 12/1/2020(MMDDYYYY) we will output a statement specifying the acceptable input format for the program.
  2. In the program below, I have taken input in such a way so that I can separate out day and month and year in separate variables in order to work on them easily.
  3. Next, I will check my first condition, if the day is less than 27 as till then irrespective of month and year we just have to increment the day by 1 and the month and year remain as they were.
    a. Next, I will check for day=28:
    If the month is Feb I will further check if it’s a leap year or not and accordingly set the date as 29 Feb or 1 march of the respective year. If it’s not Feb then I will simply increment the day by 1.
    b. Next I will check for day=29:
    If its Feb then the month will be incremented by 1 and day will be set to 1 otherwise simply increment the day by 1.
    c. Next I will check for day=30:
    For January, march, may, July, August, October, and December, I will simply increment the day by 1 otherwise I will increment the month by 1 and set the date to 1
    d. Lastly, I will check for day=31:
    If this condition is true then we will set the day to 1. Further, we will check if month is December as then we will set the month to 1 and increment the year by 1 otherwise we will just increment the month by 1.
  4. After setting the date, I will print it and before printing day and month I will check if they need to be preceded by a 0 or not.

Code:

#include <iostream>
using namespace std;

int main()
{
	int d, m, y;
	cout << "Enter today's date in the format:DD MM YYYY\n";
	cin >> d >> m >> y;
	if (d > 0 && d < 28)	//checking for day from 0 to 27
		d += 1;
	if (d == 28)
	{
		if (m == 2)	//checking for february
		{
			if ((y % 400 == 0) || (y % 100 != 0 || y % 4 == 0))	//leap year check in case of feb
			{
				d = 29;
			}
			else
			{
				d = 1;
				m = 3;
			}
		}
		else	//when its not feb
			d += 1;
	}
	if (d == 29)	//last day check for feb
	{
		if (m == 2)
		{
			d = 1;
			m = 3;
		}
		else
			d += 1;
	}
	if (d == 30)	//last day check for april,june,September,November
	{
		if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
			d += 1;
		else
		{
			d = 1;
			m += 1;
		}
	}
	if (d == 31)	//last day of the month
	{
		d = 1;
		if (m == 12)	//checking for last day of the year
		{
			y += 1;
			m = 1;
		}
		else
			m += 1;
	}
	cout << "Tomorrow's date:\n";
	if (d < 10)	//checking if day needs to be preceded by 0
	{
		cout << "0" << d << " ";
	}
	else
		cout << d << " ";
	if (m < 10)	//checking if month needs to be preceded by 0
	{
		cout << "0" << m << " ";
	}
	else
		cout << m << " ";
	cout << y;
	return 0;
}

Output:

Enter today's date in the format:DD MM YYYY

28 02 2020

Tomorrow's date:

01 03 2020 

 

Report Error/ Suggestion

Related Posts:

[yuzo_views]



Interview Questions



CopyRight © 2020

CopyRight © 2020