The following is an idea that was proposed for the solution:
#include<stdio.h>
main()
{
int yr, lp_yrs, difference, total_days, day_of_week, weekday;
printf("Enter the year:");
scanf ("%d", &yr);
difference = (yr%100); /*difference between entered year and reference year*/
lp_yrs = (yr%100) / 4; /*no. of leap years between concerned year and reference year*/
total_days = (difference*365) + lp_yrs ;
day_of_week = total_days % 7;
if (day_of_week == 0)
{
printf("The day on Jan 1st of this year is Monday");
}
else if (day_of_week ==1)
{
printf("The day on Jan 1st of this year is Tuesday");
}
else if (day_of_week == 2)
{
printf("The day on Jan 1st of this year is Wednesday");
}
else if (day_of_week == 3)
{
printf("The day on Jan 1st of this year is Thursday");
}
else if (day_of_week ==4)
{
printf("The day on Jan 1st of this year is Friday");
}
else if (day_of_week ==5)
{
printf("The day on Jan 1st of this year is Saturday");
}
else if (day_of_week ==6)
{
printf("The day on Jan 1st of this year is Sunday");
}
}
The file can be downloaded at:
Download File
Assuming that you are not expected to come up with any date earlier than the one given.
Your calculations can be fairly simple.
* Calculate the number of days between your start date and the requested date
* Find the remainder when you divide by seven
* That will give you the day of the week, where monday == 0, tuesday == 1,..., Sunday == 6.
So the crux of the problem is in step 1
Break this down into three parts
* How many whole years between the start and and end dates
* How many whole months between the start and end months
* How many day between the start and end days
Again the only tricky part is in the first step.
Each year has 365 days so multiple the number by 365. Then think about the number
of leap years, a leap year occurs every four years, so divide the number of years that you have by four.
#include<stdio.h>
main()
{
int yr, lp_yrs, difference, total_days, day_of_week, weekday;
printf("Enter the year:");
scanf ("%d", &yr);
difference = (yr%100); /*difference between entered year and reference year*/
lp_yrs = (yr%100) / 4; /*no. of leap years between concerned year and reference year*/
total_days = (difference*365) + lp_yrs ;
day_of_week = total_days % 7;
if (day_of_week == 0)
{
printf("The day on Jan 1st of this year is Monday");
}
else if (day_of_week ==1)
{
printf("The day on Jan 1st of this year is Tuesday");
}
else if (day_of_week == 2)
{
printf("The day on Jan 1st of this year is Wednesday");
}
else if (day_of_week == 3)
{
printf("The day on Jan 1st of this year is Thursday");
}
else if (day_of_week ==4)
{
printf("The day on Jan 1st of this year is Friday");
}
else if (day_of_week ==5)
{
printf("The day on Jan 1st of this year is Saturday");
}
else if (day_of_week ==6)
{
printf("The day on Jan 1st of this year is Sunday");
}
}
The file can be downloaded at:
Download File
aoa,2016 k lye answer tik nai a raha.....1/1/16
ReplyDeleteko friday ata hye lekin ye Sunday print kar raha hye....There is a logical error somewhere...
difference = (yr%100); /*difference between entered year and reference year*/
ReplyDeleteShould be
difference = (yr-1900); /*difference between entered year and reference year like 01/01/1900*/
wrong wrong
ReplyDelete(Try this one)
Delete#include
int main()
{
int yr,ref_y,diff,lpy,nmyr,td,day;
printf("Enter the ref_y & given year:");
scanf("%d%d",&ref_y,&yr);
diff=yr-ref_y;
lpy=diff/4;
nmyr=diff-lpy;
td=diff+lpy;
day=td%7;
if(day==0)
{
printf("Monday");
}
else if(day==1)
{
printf("Tuesday");
}
else if(day==2)
{
printf("Wednesday");
}
else if(day==3)
{
printf("Thursday");
}
else if(day==4)
{
printf("Friday");
}
else if(day==5)
{
printf("Saturday");
}
else if(day==6)
{
printf("Sunday");
}
return(0);
}
If i want to give input both before 1900 and after 1900 so how could we do it.
ReplyDeleteThanks and I have a nifty give: Can You Hire Someone To Renovate A House average cost to renovate a house
ReplyDelete