Friday, July 14, 2006

Calendar Program - Detecting the day on 1st Jan between 1900-19xx using if-else

This was the most tricky problem, and took me a lotta days to figure out what the solution would be like...

According to the gregorian calendar, it was Monday on the date 01/01/1900.
If any year is input through the keyboard write a program to find out what
is the day on 1st January of this year.



The following is an idea that was proposed for the solution:

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

33 comments:

Anonymous said...

Thanks for explaining the concept so systematically ..
My Peas Sized brain could not think over this !!
Thanks Again

Copper Skull said...

Thanks for the comment. Always glad to help.

Anonymous said...

Interesting - I tried this program, and it gives incorrect results. It says 1/1/2009 is on a Friday, when in fact it's Thursday. It says 1/1/2008 is on a Thursday, but it was actually on a Tuesday. In any event, I'm teaching myself C, so programs you post are helpful. Thank you!

Anonymous said...

Oooooooh - I'm a little slow on the uptake. How can we change it so that it can do any year?

suno sunao city bajao said...
This comment has been removed by the author.
suno sunao city bajao said...
This comment has been removed by the author.
suno sunao city bajao said...

please change the above written program,because that is wrong.

according to your concept[ for an example:01:01:2008,
the total day will be 8*365+2 that is wrong]

because till 2008 only one day will be added more i.e 8*365+1.

so,it;s my kindly request from all,which have need the correct concept mail me,i will surely reply.

riteshkumar00005@gmail.com

Bing Chang said...

void main()
{
float days;
int year,diff,leap,type;
long int days1;
printf("\nInput the year");
scanf("%d",&year);
year=year-1;
diff=year-1900;

if(diff<100)
{
leap=diff/4;
days=(366.0*leap)+((diff-leap)
*365+365+1);
days1=days;
type=days1%7;
}

if(diff>=100)
{
leap=(diff/4)-(diff/100)+1+
((year-2000)/400);
days=(366.0*leap)+((diff-leap)
*365+365+1);
days1=days;
type=days1%7;
}

if(type==0)
printf("Sunday");
if(type==1)
printf("Monday");
if(type==2)
printf("Tuesday");
if(type==3)
printf("Wednesday");
if(type==4)
printf("Thursday");
if(type==5)
printf("Friday");
if(type==6)
printf("Saturday");
}

Bing Chang said...

For the above,

1)The line year=year-1 was written because we are finding the days before that particular year and not that full year as the required date is 01/01/year.This means there is one less year counted as explained in (2) and (3).

2) It must not be confused that when the computer calculates for eg 1904-1900=4 years it is correct in the sense that the year 1900,1901,1902,1903 is counted but not 1904. Therefore in "days" 365 is added to count in the year 1900(not leap year)
and 1 is added to account for 1st of jan of that year.

3)
even though the computer calculates the number of years correctly i did not use 4 years for 1900-1904 as the diff would give a condition where the leap year is present(leap=diff/4) where in fact it is not since we are calulating the days of 1900,1901,1902,1903 and 1904 is not included.

Anonymous said...

bing ching is right

Anonymous said...

here is the best program i could write
/*Programmed on 31-01-2010
by Siamore*/
#include

int main()
{
int y,noy,nol,nod,d;
printf("\nenter the year");
scanf("%d",&y);
noy=y-1;/*no need to countthe entered year*/
nol=y/4;/*since nol is int fraction is discarded*/
nod=(noy*365)+nol;/*no need to multiply nol by 365 since there is only one day per leap year*/
d=nod%7;/*to find which day of week*/
if(d==1)
printf("\n\n\tmonday");
else if(d==2)
printf("\n\n\ttuesday");
else if(d==3)
printf("\n\n\twednesday");
else if(d==4)
printf("\n\n\tthursday");
else if(d==5)
printf("\n\n\tfriday");
else if(d==6)
printf("\n\n\tsaturday");
else
printf("\n\n\tsunday");
return(0);
}
hope it's right!

Anonymous said...

damn! the part is missing after pasting into the comment box

Anonymous said...

i cant seem to enter the angled brackets and stdio.h please enter it yourselves

Anonymous said...

the last program does it for any year. cheers!

Kishor Gavali said...

I made some changes in code to work for all Years

#include
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*/
if(yr>2000)
{
difference = (yr%100)+100;
lp_yrs = difference / 4; /*no. of leap years between concerned year and reference year*/
}

printf("Difference %d\n\n",difference);

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");
}

}

rock said...

thanks all of u..

ayush000 said...

I created a version of program provided 1st Jan 2001 is Monday

#include
int main()
{
int a,b,ly,ny,diff;
/*ny is no. of normal years while ly is no. of leap years*/
printf("enter year\n");
scanf("%d",&a);
diff=a-2001;
/*no. of leap years=qoutient when difference b/w years is divided by four. quotient is obtained if we subtract remainder*/
ly=(diff-(diff%4))/4;
ny=diff-ly;
/*b is no. of days*/
b=(ny*365)+(ly*366);
if(b%7==0) printf("Monday");
if(b%7==1) printf("Tuesday");
if(b%7==2) printf("Wednesday");
if(b%7==3) printf("Thursday");
if(b%7==4) printf("Friday");
if(b%7==5) printf("Saturday");
if(b%7==6) printf("Sunday");
return 0;
}

silent killer said...

/*(d) According to the Gregorian calendar, it was Monday on
the date 01/01/1900. If any year is input through the keyboard
write a program to find out what is the day on 1st January of
this year.*/
#include //header file
#include //header file
int main() //main function.. starting of c code
{
int year,differ,lp_year,day_type;
long int days;


printf("Please enter the year: ");
scanf("%d",&year);
year=year-1; //we will find days before given year so
differ=year-1900;
/*as leap year is not divisible by 100.so,create 2 condition
one difference less than 100 and greater than 100*/
if(differ<100)
{
lp_year=differ/4; //caln of total no. of leap year
days=(366*lp_year)+((differ-lp_year)*365+365+1);//see Note1
day_type=days%7; //caln of day type sun, mon......
}

if(differ>=100)
{
lp_year=(differ/4)-(differ/100)+1+((year-2000)/400);//see Note2 days=(366*lp_year)+((differ-lp_year)*365+365+1);//see Note3
day_type=days%7;
}
if(day_type==0)
printf("\nSunday");
if(day_type==1)
printf("Monday");
if(day_type==2)
printf("Tuesday");
if(day_type==3)
printf("Wednesday");
if(day_type==4)
printf("Thursday");
if(day_type==5)
printf("Friday");
if(day_type==6)
printf("Saturday");
getch(); //for holding screen till any key is pressed
return 0; //int main() is function so value must be return.
//u will read in function chapter
}

/*Note1:
-leap year has 366 day so lp_year*366
-remaining year has 365 day so (differ-lp_year)*365
-add 365 because we reduce 1 year
-add 1 to make jan 1 on which we find day type

Note2:
-(leap year come in every 4 year so) (differ/4) for leap year
-(leap year isn't divisible by 100 so we subtract (differ/100)
from counting as leap year
-(leap year will be if divisible by 400 so ((year-2000)/400)
to count that year as leap year
- we calculate from 2000 so we add 1

Note3:
-leap year has 366 day so lp_year*366
-remaining year has 365 day so (differ-lp_year)*365
-add 365 because we reduce 1 year
-add 1 to make jan 1 on which we find day type
*/

innovativedude said...

actually u r all wrong....leap years can not be counted dat easily coz

/*A year will be a leap year if it is divisible by 4 but not by 100.
If a year is divisible by 4 and by 100, it is not a leap year unless it is also divisible by 400. */

as per http://www.dataip.co.uk/Reference/LeapYear.php

so its better if u make program assuming that there is no leap year
hope m right...

innovativedude said...

so dis is best i could make...
#include
#include
void main()
{
int y1 = 1900 , y2 , day,days_left , difference_in_years , total_weeks , total_days ;

printf ( "enter a year" ) ;
scanf ( "%d", &y2 ) ;

difference_in_years = y2 - y1 ;
total_days = ( y2-y1 )*365 ;
total_weeks = total_days/7 ;
days_left = total_days % 7 ;
day = 1 + days_left ;

if(day == 1)
printf("\n monday");

else if(day == 2)
printf("\n tuesday");

else if(day == 3)
printf("\n wednesday");

else if(day == 4)
printf("\n thursday");

else if(day == 5)
printf("\n friday");

else if(day == 6)
printf("\n saturday");

else if(day == 7)
printf("\n tsunday");

getch();
}

Kriti said...

made this code in dev c++ .. n it worked well fr all years :)

#include
#include

int main(void)
{
system("cls");
int year,diff,day,normal_days, leap_days, tot_days;
printf("Enter the year \t");
scanf("%d",&year);

diff=year-1900;

normal_days= 365*diff;

if(diff<100)
{
leap_days=(diff/4);
}

if(diff>=100)
{
leap_days=(diff/4)-(diff/100)+(diff/400);
}

tot_days=normal_days+leap_days;
day=(tot_days)%7;

if(day==0)
printf("Monday");
if(day==1)
printf("Tuesday");
if(day==2)
printf("Wednesday");
if(day==3)
printf("Thursday");
if(day==4)
printf("Friday");
if(day==5)
printf("Saturday");
if(day==6)
printf("Sunday");

getch();
return 0;
}

aman kumar singh said...

Its very easy guys just go to
http://amancybertricks.blogspot.in/2012/02/calender-program-in-c.html

Anonymous said...

Write a program that determines the day number (1 to 366) in a year for a date that is provided as input data. As an example, January 1, 2011 is day 1. December 31, 2010 is a day 365. December 31, 2012 is day 366, since 2012 is a leap year. A year is a leap year if it is divisible by four, except that any year divisible by 100 is a leap year only if it is divisible 400. Your program should accept the month, day and year as integers.

plzzz write pseudo codes n c++ program....i need it urgently plzzz

Anonymous said...

I wrote this algorithm for the question (i'm just starting out in C). I THINK it's correct:

#include
int main(void)
{
int a, b=1900,c=1904, leap, year;
long d, total_days;
printf("Enter a year:\n");
scanf("%d",&a);
d = a;
d = d-b;
year = a;
printf("Calculating.. %ld",d*365);
if((a % 4 == 0 && a % 100 != 0) || (a % 100 == 0 && a % 400 == 0))
leap = 0;
if(a % 4 == 1)
leap = 1;
if(a%4 == 2)
leap = 2;
if (a%4 == 3)
leap = 3;
year = year - leap;
year = (year - c) / 4;
total_days = (d * 365) + year;
printf("%ld %d\n",total_days,leap);

if (leap == 0) {
switch(total_days % 7) {
case 0: printf("It is Monday");
break;
case 1: printf("It is Tuesday");
break;
case 2: printf("It is Wedn");
break;
case 3: printf("It is Thu");
break;
case 4: printf("It is Fri");
break;
case 5: printf("It is Sat");
break;
case 6: printf("It is Sunday");
break;
}
}
else if (leap != 0) {
switch(total_days % 7) {
case 6: printf("It is Monday");
break;
case 0: printf("It is Tuesday");
break;
case 1: printf("It is Wedn");
break;
case 2: printf("It is Thu");
break;
case 3: printf("It is Fri");
break;
case 4: printf("It is Sat");
break;
case 5: printf("It is Sunday");
break;
}
}
return 0;
}

Anonymous said...

Hey Bing Chang its not working for years > 1990, if we change 366.0 to 366 in the expression of calculation of days.
Can u xplain??

Malic Noor said...

Hi.
Here is the coding according to which it can calculate the day beyond 2012
http://www.searchforancestors.com/utility/dayofweek.html
Using This link u can counter check my result.

Here Is A code.

# include
# include
# include
using namespace std;
main()
{
int gye,ye,le,day,hye;
cout<<"Enter The year :";
cin>>gye;
le=gye%28;

hye=gye%2800;

ye=gye%7;

if(hye>=1 && hye<=100)
{ye=ye-0;}
else if(hye>=101 && hye<=200)
{ye=ye-1;}
else if(hye>=201 && hye<=300)
{ye=ye-2;}
else if(hye>=301 && hye<=400)
{ye=ye-3;}
else if(hye>=401 && hye<=500 )
{ye=ye-3;}
else if(hye>=501 && hye<=600)
{ye=ye-4;}
else if(hye>=601 && hye<=700)
{ye=ye-5;}
else if(hye>=701 && hye<=800)
{ye=ye-6;}
else if(hye>=801 && hye<=900)
{ye=ye-6;}
else if(hye>=901 && hye<=1000)
{ye=ye-7;}
else if(hye>=1001 && hye<=1100)
{ye=ye-8;}
else if(hye>=1101 && hye<=1200)
{ye=ye-9;}
else if(hye>=1201 && hye<=1300)
{ye=ye-9;}
else if(hye>=1301 && hye<=1400)
{ye=ye-10;}
else if(hye>=1401 && hye<=1500)
{ye=ye-11;}
else if(hye>=1501 && hye<=1600)
{ye=ye-12;}
else if(hye>=1601 && hye<=1700)
{ye=ye-12;}
else if(hye>=1701 && hye<=1800)
{ye=ye-13;}
else if(hye>=1801 && hye<=1900)
{ye=ye-14;}
else if(hye>=1901 && hye<=2000)
{ye=ye-15;}
else if(hye>=2001 && hye<=2100)
{ye=ye-15;}
else if(hye>=2101 && hye<=2200)
{ye=ye-16;}
else if(hye>=2201 && hye<=2300)
{ye=ye-17;}
else if(hye>=2301 && hye<=2400)
{ye=ye-18;}
else if(hye>=2401 && hye<=2500)
{ye=ye-18;}
else if(hye>=2501 && hye<=2600)
{ye=ye-19;}
else if(hye>=2701 && hye<=2800 || hye==0)
{ye=ye-20;}

if(le>=1 && le<=4)
{
day=ye+0;
}
else if(le>=5 && le<=8)
{
day=ye+1;
}
else if(le>=9 && le<=12)
{
day=ye+2;
}
else if(le>=13 && le<=16)
{
day=ye+3;
}
else if(le>=17 && le<=20)
{
day=ye+4;
}
else if(le>=21 && le<=24)
{
day=ye+5;
}
else if(le>=25 && le<=27 || le==0)
{
day=ye+6;
}

if(day%7==1|| day%7==(-6) )
cout<<endl<<"The day on 1st January of year "<<gye<<" is Monday\n";
else if(day%7==2 || day%7==(-5) )
cout<<endl<<"The day on 1st January of year "<<gye<<" is Tuesday\n";
else if(day%7==3 || day%7==(-4) )
cout<<endl<<"The day on 1st January of year "<<gye<<" is Wednesday\n";
else if(day%7==4 || day%7==(-3) )
cout<<endl<<"The day on 1st January of year "<<gye<<" is Thursday\n";
else if(day%7==5 || day%7==(-2))
cout<<endl<<"The day on 1st January of year "<<gye<<" is Friday\n";
else if(day%7==6 || day%7==(-1))
cout<<endl<<"The day on 1st January of year "<<gye<<" is Saturday\n";
else if(day%7==0)
cout<<endl<<"The day on 1st January of year "<<gye<<" is Sunday\n";
getch();
}

Lavish Kothari said...

I also have a program to find the week day
visit
http://codingloverlavi.blogspot.in/2013/03/determination-of-day-of-week-through.html

Lavish Kothari said...

I also have a program to find the week day
visit
http://codingloverlavi.blogspot.in/2013/03/determination-of-day-of-week-through.html

randi said...

teri maa ka chut!!!!!!!!!!!!!
galat hai bhosadi ke!!!!!

Anonymous said...

#include
#include
int year,odd_day,i;
long days = 0;
void main()
{
clrscr();
printf("\nEnter the year: ");
scanf("%d",&year);
for(i=1900;i<year;i++)
{
if(i%4==0)
days=days+366;
else
days=days+365;
}
odd_day = days % 7;
printf("\nJanuary 1st of %d was ",year);
if(odd_day==1)
printf("Monday");
else if(odd_day==2)
printf("Tuesday");
else if(odd_day==3)
printf("Wednesday");
else if(odd_day==4)
printf("Thursday");
else if(odd_day==5)
printf("Friday");
else if(odd_day==6)
printf("Saturday");
else
printf("Sunday");
}

Rajat Kalyan said...

i know this is very much right



#include
#include
main()

{
int day_of_week, diff ,no_of_lp_yr ,yr ,tod;
printf("Enter the year");
scanf("%d",&yr);
diff= yr-2001;
no_of_lp_yr=(yr%100)/4;
tod=diff*365+no_of_lp_yr;
day_of_week=tod%7;

if(day_of_week==0)
{

printf("the day is monday");
}
else if(day_of_week==1)
{

printf("the day is tuesday");
}
else if(day_of_week==2)
{

printf("the day is wednesday");
}
else if(day_of_week==3)
{

printf("the day is thursday");
}
else if(day_of_week==4)
{

printf("the day is friday");
}
else if(day_of_week==5)
{

printf("the day is saturday");
}
else if (day_of_week ==6)
{

printf("the day is sunday");
}

getch();

}

Anonymous said...

you are correct

Mayukh Datta said...

How to find out the first day of february or march or any other month???