C program to find the day of the week

C program to find the day of the week

Here I have provided a copy-paste code to find the day of the week in C language. Remember this program works best using Turbo C 3.0. You may make some changes to make it work with the complier you use. I have also added some comments which is for you to understand. One can cast off all the comments without affecting the working of the program. Also remember that this is not the only way to write the C program to calculate the day of the week. You may try writing simpler programs.
The program given below is with respect to the international switchover from 4th October 1582 to 15th October 1582 which is also used for astronomical calculations. Make the required changes for other switchovers. Hint make changes in line number 3 and 5 in the jg function.

#include<stdio.h>
#include<conio.h>
#include<math.h>
jg(int d, int m,int y) //Julian or Gregorian
{ int type;
if(y<1582 || (y==1582 && m<10) || (y==1582 && m==10 && d<5))
type=0; // 0 stands for Julian
else if(y>1582 || (y==1582 && m>10) || (y==1582 && m==10 && d>14))
type=1; // 1 stands for Gregorian
else
type=2; // 2 stands for neither of the two
return type;
}
fm(int d, int m,int y) //function of month
{ int fmonth,leap;
if(jg(d,m,y)==0) //for Julian calendar
{
if(y%4==0) //leap function 1 for leap & 0 for non-leap
leap=1;
else
leap=0;
}
if(jg(d,m,y)==1) //for Gregorian calendar
{
if((y%100==0) && (y%400!=0)) //leap function 1 for leap & 0 for non-leap
leap=0;
else if(y%4==0)
leap=1;
else
leap=0;
}
fmonth=3+(2-leap)*((m+2)/(2*m))+(5*m+m/9)/2; //f(m) formula
fmonth%=7; //f(m) is brought in range of 0 to 6
return fmonth;
}
c(int y) //function of century
{
int century;
century=y/100; //returns the first two digits of year
return century;
}
yy(int y) //function of year
{
int lastdigits;
lastdigits=y%100; //returns the last two digits of year
return lastdigits;
}
day_of_week(int d, int m, int y)
{
int dow,z; //dow stands for day of week
printf("\nDate: %d/%d/%d\n\n",d,m,y);
if(jg(d,m,y)==0)
{
z=y; //z is a new year variable
if(y<0)
{
z=y+9800; //z is made positive if negative, 9800 is multiple of 7
}
dow=1.25*z+fm(d,m,y)+d-2; //function of weekday for Julian
dow%=7; //remainder on division by 7
}
else
{
dow=1.25*yy(y)+fm(d,m,y)+d-2*(c(y)%4); //function of weekday for Gregorian
dow%=7; //remainder on division by 7
}
switch (dow)
{
case 0:
printf("weekday = Saturday");
break;
case 1:
printf("weekday = Sunday");
break;
case 2:
printf("weekday = Monday");
break;
case 3:
printf("weekday = Tuesday");
break;
case 4:
printf("weekday = Wednesday");
break;
case 5:
printf("weekday = Thursday");
break;
case 6:
printf("weekday = Friday");
break;
default:
printf("Incorrect data");
}
return 0;
}
void main()
{
int date,month,year;
char con='y'; //con = continue
while(con=='y' || con=='Y')
{
clrscr(); //clears screen
printf("Enter the year ");
scanf("%d",&year); //year input
printf("Enter the month ");
scanf("%d",&month); //month input
printf("Enter the date ");
scanf("%d",&date); //date input
day_of_week(date,month,year);
printf("\n\nDo you wish to continue? (Y/N) ");
scanf("%s",&con);
}
}

1 comment:

  1. This comment has been removed by the author.

    ReplyDelete