# Algorithm for the calender?

59 views

Suppose given a date how can we find out the day i.e. sunday, monday .....etc.

posted Aug 18, 2014

+1 vote
``````#include <stdio.h>
#include <time.h>   // For mktime(),  strftime and struct tm
#include <string.h> // For memset()

int main()
{
int month;
int day;
int year;
char daybuf[20];
struct tm time_str;

printf("Enter the year (>= 1900) : ");
scanf("%d", &year);
printf("Enter the month number (1 -12) : ");
scanf("%d", &month);
printf("Enter the day number (1 - 31) : ");
scanf("%d", &day);

printf("Your enterd date is (dd/mm/yy) : %d/%d/%d\n", day, month, year);

memset(&time_str, 0, sizeof(time_str));

time_str.tm_year = year - 1900;     // Year since 1900
time_str.tm_mon = month - 1;        // Month of year [0, 11]
time_str.tm_mday = day;
time_str.tm_isdst = -1;
/*
The value of tm_isdst shall be positive if Daylight Savings Time is in effect, 0 if Daylight Savings Time
is not in effect, and negative if the information is not available.
*/

if (mktime(&time_str) == -1)
printf("-Unknown-");
else {
strftime(daybuf, sizeof(daybuf), "%A", &time_str);
/*
do a "man 3 strftime"
*/
printf("\nOutput Day : %s\n", daybuf);
}
return 0;
}
``````