# Thread: Calendar program that calculates a given day on a given date

1. ## Calendar program that calculates a given day on a given date

Hi,

Though an easy one I made this program with my own logic.The program determines what day it is on a particular date For ex- Thursday on 12 july 2012.

I tested it and gave the correct output. Hope it gives the correct output always

Code:
```#include<stdio.h>
#include<conio.h>

/*Function to calculate whether the entered date is correct or not*/

int correct_date_calc(int day, int month, int year)
{
const int ref_year=2012;
int lp_yr_flg=0;

if(year<=2012)
{
if(((ref_year - year)%4) == 0)
{
lp_yr_flg=1;
}
}

else if(year>2012)
{
if(((year - ref_year)%4) == 0)
{
lp_yr_flg=1;
}
}

if(month>12 || month<1)
{
return 0;
}

if((lp_yr_flg==1) && (month==2) && (day>29))
{
return 0;
}

else if((lp_yr_flg==0) && (month==2) && (day>28))
{
return 0;
}

else if(((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12)) && (day>31 || day<1))
{
return 0;
}

else if(((month == 4) || (month == 6) || (month == 9) || (month ==11)) && (day>30 || day<1))
{
return 0;
}

return 1;
}

/*Function to Calculate leap year*/

int leap_year_calc(int year_lp)
{
const int ref_year=2012;
int lp_yr_flg=0;

if(year_lp<=2012)
{
if(((ref_year - year_lp)%4) == 0)
{
lp_yr_flg=1;
return lp_yr_flg;
}
}

else if(year_lp>2012)
{
if(((year_lp - ref_year)%4) == 0)
{
lp_yr_flg=1;
return lp_yr_flg;
}
}

return 0;
}

/*Fucntion to Calculate the given days in a month*/

int no_of_days(int month, int year)
{
int day=0;
int leap_yr = 0;

leap_yr = leap_year_calc(year);
if((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12))
{
day=31;
return day;
}

else if((month == 4) || (month == 6) || (month == 9) || (month ==11))
{
day=30;
return day;
}

else if((month == 2) && (leap_yr == 1))
{
day=29;
return day;
}

else if((month == 2) && (leap_yr == 0))
{
day=28;
return day;
}
}

/*Function to calculate the day before the reference date. Here it is 31/12/2012.*/

int bef_ref_date(int day_bef,int month_bef,int year_bef)
{

int day_dec,year_dec,month_dec=12,day=1;
for(year_dec=2012; year_dec>=year_bef; year_dec--)
{
for(month_dec=12; month_dec>=1; month_dec--)
{
for(day_dec=no_of_days(month_dec, year_dec); day_dec>=1; day_dec--)
{
if(year_dec==year_bef && month_dec==month_bef && day_dec==day_bef)
{
return day;
}

day--;
if(day==0)
{
day=7;
}
}
}
}
}

/*Function to calculate the day after the reference date. Here it is 1/1/2012.*/

int aft_ref_date(int day_aft,int month_aft,int year_aft)
{

int day_inc, year_inc, month_inc=1, day=1, month_day_lim;
for(year_inc=2012; year_inc<=year_aft; year_inc++)
{
for(month_inc=1; month_inc<=12; month_inc++)
{
month_day_lim = no_of_days(month_inc, year_inc);

for(day_inc=1; day_inc<=month_day_lim; day_inc++)
{
if(year_inc==year_aft && month_inc==month_aft && day_inc==day_aft)
{
return day;
}

day++;
if(day==8)
{
day=1;
}
}
}
}
}

void main()
{
int day, month, year, day_date, crct_date_flg=0;
const int ref_year = 2012;

printf("Enter the Day ");
scanf("%d",&day);

printf("\nEnter the Month ");
scanf("%d",&month);

printf("\nEnter the year ");
scanf("%d",&year);

crct_date_flg = correct_date_calc(day, month, year);
clrscr();
if(crct_date_flg == 0)
{
printf("Date Entered is an Invalid Date!!");
getch();
}

clrscr();
if(crct_date_flg==1)
{
if(year <= ref_year)
{
day_date = bef_ref_date(day, month, year);

switch (day_date)
{
case 1:
printf("The day was Monday");
break;
case 2:
printf("The day was Tuesday");
break;
case 3:
printf("The day was Wednesday");
break;
case 4:
printf("The day was Thursday");
break;
case 5:
printf("The day was Friday");
break;
case 6:
printf("The day was Saturday");
break;
case 7:
printf("The day was Sunday");
break;
}
}

else if (year > ref_year)
{
day_date = aft_ref_date(day, month, year);

switch (day_date)
{
case 1:
printf("The day is Sunday");
break;
case 2:
printf("The day is Monday");
break;
case 3:
printf("The day is Tuesday");
break;
case 4:
printf("The day is Wednesday");
break;
case 5:
printf("The day is Thursday");
break;
case 6:
printf("The day is Friday");
break;
case 7:
printf("The day is Saturday");
break;
}
}

getch();
clrscr();
}
}```

2. I'm assuming there's no question associated with this post?

Well, it gave the right day for my birthday in four years - after eliminating the non-standard header "conio.h" and all associated (and not-required) "getch()" and "clrscr()" calls.

Just a few nit-picks:
- "void main()" should be "int main(void)" ... and you should "return 0" at the end of the main function
- All your functions are defined to return an integer value, though most of them don't - though it was difficult to tell at first glance because;
- The indentation in this program (one space!?) made it difficult to scan easily by eye

However, the value of this program lies in what you've learned. If you've learned a good deal, and got some more experience, then you're doing well. My criticisms are intended to be helpful, and hope they're taken in that spirit.

3. Perhaps compare your results with the standard library (in some way).
Code:
```#include <stdio.h>
#include <time.h>

int main ( ) {
struct tm now = { 0 };
int numDays = 0;

// start test at 19960101
now.tm_year = 1996 - 1900;
now.tm_mon = 0;
now.tm_mday = 1;
mktime(&now);

// finish test at 20041231
while ( now.tm_year < (2005-1900) ) {
// call your test(s) with now.tm... fields

// now try the next day
numDays++;
now.tm_mday++;
mktime(&now);
}
printf("%d days counted\n", numDays);
return 0;
}```