Computer Science, asked by sayandas9647, 1 year ago

Write a c program to display the next date when you enter date, month, year .

Answers

Answered by CBSEMP
6
This is the program which can be used to find the next date of a given date. It works as follows. If the given date is the ending of a month, date becomes 1 (start of next month), month increases by 1, year remains same. If the given date is the ending of an year, date becomes 1 (start of next year), month becomes 1 (start of next year), year increases by 1. Also, the almost perfect definition of leap year (divisible by 4, not a multiple of 100 but multiple of 400) is considered.

Program
#include<stdio.h>
#include<conio.h>
void main(void)
{
printf("\n Enter a date : ");
scanf("%d-%d-%d",&d1,&m1,&y1);
switch(m1)
{
case 1:
if(d1==31)
{
d2=1;
m2=m1+1;
y2=y1;
}
else
{
d2=d1+1;
m2=m1;
y2=y1;
}
break;
case 2:
if(y1%4==0&&(y1%100!=0||y1%400==0))
{
if(d1==29)
{
d2=1;
m2=m1+1;
y2=y1;
}
else
{
d2=d1+1;
m2=m1;
y2=y1;
}
}
else
{
if(d1==28)
{
d2=1;
m2=m1+1;
y2=y1;
}
else
{
d2=d1+1;
m2=m1;
y2=y1;
}
}
break;
case 3:
if(d1==31)
{
d2=1;
m2=m1+1;
y2=y1;
}
else
{
d2=d1+1;
m2=m1;
y2=y1;
}
break;
case 4:
if(d1==30)
{
d2=1;
m2=m1+1;
y2=y1;
}
else
{
d2=d1+1;
m2=m1;
y2=y1;
}
break;
case 5:
if(d1==31)
{
d2=1;
m2=m1+1;
y2=y1;
}
else
{
d2=d1+1;
m2=m1;
y2=y1;
}
break;
case 6:
if(d1==30)
{
d2=1;
m2=m1+1;
y2=y1;
}
else
{
d2=d1+1;
m2=m1;
y2=y1;
}
break;
case 7:
if(d1==31)
{
d2=1;
m2=m1+1;
y2=y1;
}
else
{
d2=d1+1;
m2=m1;
y2=y1;
}
break;
case 8:
if(d1==31)
{
d2=1;
m2=m1+1;
y2=y1;
}
else
{
d2=d1+1;
m2=m1;
y2=y1;
}
break;
case 9:
if(d1==30)
{
d2=1;
m2=m1+1;
y2=y1;
}
else
{
d2=d1+1;
m2=m1;
y2=y1;
}
break;
case 10:
if(d1==31)
{
d2=1;
m2=m1+1;
y2=y1;
}
else
{
d2=d1+1;
m2=m1;
y2=y1;
}
break;
case 11:
if(d1==30)
{
d2=1;
m2=m1+1;
y2=y1;
}
else
{
d2=d1+1;
m2=m1;
y2=y1;
}
break;
case 12:
if(d1==31)
{
d2=1;
m2=1;
y2=y1+1;
}
else
{
d2=d1+1;
m2=m1;
y2=y1;
}
break;
}
printf("\n Given Date : %d-%d-%d",&d1,&m1,&y1);
printf("\n Next Date : %d-%d-%d",&d2,&m2,&y2);
getch();
}

Output
Enter a date : 09-12-1996
Given Date : 09-12-1996
Next Date : 10-12-1996
OR
Enter a date : 28-02-1996
Given Date : 28-02-1996
Next Date : 29-02-1996
OR
Enter a date : 31-12-1996
Given Date : 31-12-1996
Next Date : 01-01-1997

If you create a function which returns the number of days a month has, the program can be highly simplified. I didn't do that simplification because the program can be understood very clearly if it is like this.

Hope this program helps.
Similar questions