0% found this document useful (0 votes)
8 views

Assignment Pps Explatation

Uploaded by

gracetristen0
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Assignment Pps Explatation

Uploaded by

gracetristen0
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 4

PRINCE AYUSH

RA2011027010028

EXPLANATION OF THE CODE: -


CONCEPTS USED = loop and if else statements

We need to take the input of the date in the format of dd/mm/yyyy so for that we initialize 3
variable if int type and name them as d,m,y. Now ww use scanf function to take input of all three
variable and now we will use printf function to display them. Now we will initialize 3 variable of
int type and name them as pd,pm,py and now we will check of the d variable that if it is equal to 1
then previous date and month both will change so we will use if else statements to figure it out.

If out current date is d=1 then pd will be 30,31,or 28 or 29 depends on the month so now we will
use if else for months variable that is m, if our m=2,4,6,8,911 then our pd will be 31 and if our
m=5,7,10,12 then our pd will 30 and if our m=3 then out pd can be 28 or 29 depends on leap year
and we know that leap year is multiple of 4 so we will put a if condition as y%4==0 then our pd
will 29 or else pd will be 28 and in all above cases year will be as current year py=y. Now if m=1
then out pd will be 31 because December last date is 31 and m will be 12 because December is 12 th
month of the year and py will be y-1, so these all are the condition for the previous date which we
will use a printf function to display them.

Now we will initialize 3 variable of int type nd,nm,ny now if our current date is 30 if our
m=4,6,9,11 then our nd will be 1 and nm = m+1 and if other months are there then nd will be 31
and month will be same and if our date is 31 then our nd will be 1 and nm will be m+1 and if date is
28 or 29 then nd will be 1 and nm will be m+1 and if d=31 and m=12 then nd=1 and nm =1 and y=
y+1.

And if we will put all this above if else in a for loop of int i=0 and i<=5 and i++ then it will run 5
times and we can take input all 5 inputes at a single time and display at a time only.

CODE: -

#include<stdio.h>
int main()
{
int current_date,current_month,current_year,previous_date,previous_month,previous_year,next_date,next
_month,next_year;
for(int i=1;i<=5;i++)
{
printf("%d Run",i);
printf("\nEnter date in dd/mm/yyyy fromat:");
scanf("%d %d %d",&current_date,&current_month,&current_year);
printf("Current (Today's)date is:");
printf("%0d/%0d/%0d",current_date,current_month,current_year);
if(current_date==1)
{
if(current_month==4||current_month==6||current_month==9||current_month==11)
{
previous_date=31;
previous_month=current_month-1;
previous_year=current_year;
}
else if(current_month==3)
{
if(current_year%4==0)
{
previous_date=29;
previous_month=current_month-1;
previous_year=current_year;
}
else
{
previous_date=28;
previous_month=current_month-1;
previous_year=current_year;
}
}
else if(current_month==5||current_month==7||current_month==8||current_month==12)
{
previous_date=30;
previous_month=current_month-1;
previous_year=current_year;
}
else if (current_month==1)
{
previous_date=31;
previous_month=12;
previous_year=current_year-1;
}
}
else
{
previous_date=current_date-1;
previous_month=current_month;
previous_year=current_year;
}
if(current_date==30)
{
if(current_month==4||current_month==6||current_month==9||current_month==11)
{
next_date=1;
next_month=current_month+1;
next_year=current_year;
}
else
{
next_date=31;
next_month=current_month;
next_year=current_year;
}
}
else if(current_date==31)
{
if(current_month==12)
{
next_date=1;
next_month=1;
next_year=current_year+1;
}
else
{
next_date=1;
next_month=current_month+1;
next_year=current_year;
}
}
else if(current_date==28||current_date==29)
{
next_date=1;
next_month=current_month+1;
next_year=current_year;
}
else
{
next_date=current_date+1;
next_month=current_month;
next_year=current_year;
}
printf("\nPrevious (Yesterday's)date is:");
printf("%d/%d/%d",previous_date,previous_month,previous_year);
printf("\nNext (Tomorrow's)date is:");
printf("%d/%d/%d\n",next_date,next_month,next_year);
}
return 0;
}

OUTPUT: -

You might also like