# C Program to get the Day of a given Date

C Programming Language is one of the most important and simple programming languages. The object oriented programming languages like C++ and Java can be easily understood after learning C. Hence, I am writing a C Program to obtain the day of a given date. Also, this is my first article.

## CALENDAR INFORMATION

An year is the time taken by Earth to revolve around the Sun once. It is almost equal to 365.25 days. Hence, if 365 days are considered as an year, 1 day is left from the total of 4 years. This leads to a leap year which occurs once in every 4 years and contains 366 days. Starting from the year 0001, every multiple of 4 like 0004, 0008 etc., is a leap year. But the value of 365.25 is also approximate. This leads to anomalies in every 100 years or 400 years i.e. year 0100, 0200, 0300 are not leap years but 0400 is a leap year.

There are 12 months in an year. Their lengths (Number of Days) are as follows:
January - 31
February - 28 (Non Leap Year) & 29 (Leap Year)
March - 31
April - 30
May - 31
June - 30
July - 31
August - 31
September - 30
October - 31
November - 30
December - 31
Therefore, the TOTAL = 365 (Non Leap Year) & 366 (Leap Year).

By going backward, I found a fact that is 01-01-0001 i.e. 1st January 0001 is MONDAY. This serves as basement to the following program. We also use a variable called day_value which is equal to 0,1,2,3,4,5 or 6 for Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday respectively.

## PROGRAM

#include"stdio.h"
#include"conio.h"
void print_day(int f)
{
if (f == 0)
printf("\n MONDAY \n");
else if (f == 1)
printf("\n TUESDAY \n");
else if (f == 2)
printf("\n WEDNESDAY \n");
else if (f == 3)
printf("\n THURSDAY \n");
else if (f == 4)
printf("\n FRIDAY \n");
else if (f == 5)
printf("\n SATURDAY \n");
else
printf("\n SUNDAY \n");
}
void main(void)
{
int a, b, c, d;
long int e;
clrscr();
printf("\n Enter a valid date in dd-mm-yyyy format:");
scanf("%d-%d-%d", &a, &b, &c);
for (d = 1, e = (365 * (c - 1)) + (a - 1) + ((c / 4) - (c / 100) + (c / 400)); d < b; d++)
e += (d == 2) ? (28) : ((d == 4 || d == 6 || d == 9 || d == 11) ? (30) : (31));
print_day(e % 7);
getch();
}

## EXPLANATION

There are two header files. They are:
#include"stdio.h" : STANDARD INPUT AND OUTPUT
#include"conio.h" : CONDITIONAL INPUT AND OUTPUT
The former is essential for the functions like printf - print data, scanf - scan data etc,.
The latter is essential for the functions like clrscr - clear screen, getch - get character etc,.

### FUNCTIONS

There are two functions. They are:
void print_day(int) : Prints day corresponding to the day_value.
void main(void) : Essential in every C program. This contains the code from which execution starts.
'MAIN' FUNCTION
The main() function code can be explained as follows. There are:
1. Two lines for declarations.
2. Two lines for scanning the date.
3. Two lines for calculating the day_value.
4. One line to print the day of a day_value.
Consider the two lines which are used to calculate the day_value.
for (d = 1, e = (365 * (c - 1)) + (a - 1) + ((c / 4) - (c / 100) + (c / 400)); d < b; d++)
e += (d == 2) ? (28) : ((d == 4 || d == 6 || d == 9 || d == 11) ? (30) : (31));
It can be explained as follows.
Let the date given be 'a-b-c'.
Loop Variable 'd' is used.
Number of days from '01-01-0001' to 'a-b-c' is stored in 'e'.
The variable 'e' is initialized to (365 * (c - 1)) + (a - 1) + ((c / 4) - (c / 100) + (c / 400))
a) (365 * (c - 1)) : Number of days if all previous years are considered normal years.
b) (a - 1) : Number of days in given month until given day.
c) ((c / 4)) : Number of days after considering general definition of leap year.
d) (- (c / 100) + (c / 400)) : Number of days after considering complete definition of leap year.
Now, the days left are the days in months until 'b'. The loop calculates those values and adds it to 'e'. The loop adds 28 days if d=2 i.e. February and 31 days if d=1 i.e. January and so on.
Hence, all the days until 'a-b-c' are counted.
'PRINT_DAY' FUNCTION
Using the fact that 01-01-0001 is MONDAY, we can say that, if 'e' is a multiple of 7, the day of the given date is MONDAY. Hence, the remainder obtained when 'e' is divided by 7 is the day_value. This function prints the day corresponding to the day_value. Hence, our work is done.