c program to find weekday of a given date


A real time Program is an Integration of all the constructs what we have learned until now. Hence if your practice on the previous sessions are practically proper then we can attempt any logic which cal make intelligent program. Let us take a test. Let us write a Program to find the WEEK DAY of a Given DATE.
Program
#include<stdio.h>
void main()
{
int d, m, y, n, nleap, nyear, nord, nodd1, nodd2, day, j, sum;
printf("\nEnter Any Date (DD-MM-YYYY) Format Only : ");
scanf("%d-%d-%d", &d, &m, &y);
printf("\n\nDate Entered is : %d - %d - %d", d, m, y);
if(y >= 1600 && y <= 1900)
{
day = 0;
nyear = y - 1600;
}
if(y >= 1901 && y <= 2400)
{
day = 1;
nyear = y - 1901;
}
nleap = nyear / 4;
nord = nyear - nleap;
nodd1 = 2 * nleap + 1 * nord;
day += nodd1 % 7;
sum = 0;
for(j = 1; j < m; j++)
{
switch(j)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
sum += 31;
break;
case 4: case 6: case9: case 11:
sum += 30;
break;
case 2:
if(y % 4 == 0)
sum += 29;
else
sum += 28;
}
}
sum += d;
nodd2 = sum % 7;
day += nodd2;
day %= 7;
switch(day)
{
case 0:
printf("\nDay is Sunday ");
break;
case 1:
printf("\nDay is Monday ");
break;
case 2:
printf("\nDay is Tuesday ");
break;
case 3:
printf("\nDay is Wednesday ");
break;
case 4:
printf("\nDay is Thursday ");
break;
case 5:
printf("\nDay is Friday ");
break;
case 6:
printf("\nDay is Saturday ");
break;
}
}

No comments: