Calender Program in C Programming Language :
Program will accept Year,Month and Date from the user and will display the day of the month.
#include<stdio.h> #include<conio.h> #include<math.h> int isdatevalid(int month, int day, int year) { if (day <= 0) return 0 ; switch( month ) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (day > 31) return 0 ; else return 1 ; case 4: case 6: case 9: case 11: if (day > 30) return 0 ; else return 1 ; case 2: if ( day > 29 ) return 0 ; if ( day < 29 ) return 1 ; else return 0 ; } return 0 ; } //------------------------------------------------ int fm(int date, int month,int year) { int fmonth,leap; //leap function 1 for leap & 0 for non-leap if((year%100==0) && (year%400!=0)) leap=0; else if(year%4==0) leap=1; else leap=0; fmonth=3+(2-leap)*((month+2)/(2*month))+(5*month+month/9)/2; //f(m) formula fmonth = fmonth % 7; //bring it in range of 0 to 6 return fmonth; } //---------------------------------------------- int day_of_week(int date, int month, int year) { int dow; //day of week int YY = year % 100; int century = year / 100; printf("nDate: %d/%d/%dnn",date,month,year); dow = 1.25 * YY + fm(date,month,year) + date - 2*( century % 4); //function of weekday for Gregorian dow = 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; clrscr(); printf("Enter the year "); scanf("%d",&year); printf("Enter the month "); scanf("%d",&month); printf("Enter the date "); scanf("%d",&date); day_of_week(date,month,year); getch(); }
Output :
Enter the year 2012 Enter the month 02 Enter the date 29 Date: 29/2/2012 weekday = Wednesday
No comments:
Post a Comment