Q:

C++ program to get previous date of given date

0

C++ program to get previous date of given date

C++ program to find the previous date: this program will take a date and print its previous date.

Given a date in DD, MM and YYYY format, you have to find the previous date using C++ program.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C++ program/code to get previous date of given date:

#include <iostream>
using namespace std;

//fnction to get previous date
void getPrevDate(int *day,int *month, int *year)
{
	//if there is first day of month
	if(*day==1)
	{
		//months which have 30 days in previous month
		if(*month==4|| *month==6|| *month==9|| *month==11)
		{
			*day=31;
			*month = *month -1;	
		}
		//for MARCH, to define february last day
		else if(*month==3)
		{
			if(*year%4==0)	
				*day=29;
			else
				*day=28;
			
			*month = *month -1;
		}
		//for January, to define December last day
		else if(*month==1)
		{
			*day=31;
			*month = 12;
			*year = *year - 1 ;
		}
		//for Feb, to define January last day
		else if(*month==2)
		{
			*day=31;
			*month = *month -1;
		}
		//for other months
		else
		{
			*day=30;
			*month = *month -1;
		}
	}
	//other days of month
	else
	{
		*day = *day-1;
	}	

}

int main()
{
	//date, assigning day, month and year
	int dd=1,mm=1,yy=2017;
	//printing given date
	cout<<endl<<"Date : "<<dd<<"/"<<mm<<"/"<<yy;
	//function call to get previous date
	getPrevDate(&dd,&mm, &yy);
	//printing previous date
	cout<<endl<<"Previous date : "<<dd<<"/"<<mm<<"/"<<yy;
	
	return 0;    
}

Output

Date : 30/6/2017
Previous date : 29/6/2017

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Most popular and Searched C++ solved programs with Explanation and Output

Similar questions


need a help?


find thousands of online teachers now
Reading date and time from Linux operating system ... >>
<< C++ program to find total number of days in given ...