ccplusplus.com
Learn C, C++ Concepts
Monday, September 26, 2011
c program to calculate weekdays
/*********************************************************************** * File : weekday-calculator.c * Author : Saurabh Gupta * Desc : weekday calculator in c * c program to calculate weekday * Source : http://saurabhgupta0527.blogspot.com/p/c.html * Created : PM 09:33 26 September 2011 * Note : see usage of mktime time function ************************************************************************/ #include <stdio.h> #include <time.h> int main () { printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); printf ("XXXXXXXX WEEKDAY CALCULATOR XXXXXX\n"); printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); time_t rawtime; struct tm * stTimeInfo; int nYear, nMonth, nDay; char * weekday[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; /* * take the user date */ printf ("Enter nYear: "); scanf ("%d",&nYear); printf ("Enter nMonth: "); scanf ("%d",&nMonth); printf ("Enter nDay: "); scanf ("%d",&nDay); /* * get current stTimeInfo and modify it to the user's choice */ time ( &rawtime ); stTimeInfo = localtime ( &rawtime ); stTimeInfo->tm_year = nYear - 1900; stTimeInfo->tm_mon = nMonth - 1; stTimeInfo->tm_mday = nDay; /* * call mktime: stTimeInfo->tm_wday will be set */ mktime ( stTimeInfo ); printf ("Entered Day is a %s.\n", weekday[stTimeInfo->tm_wday]); printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); return 0; } /* * OUTPUT * [sgupta@rhel55x86 c]$ gcc weekday-calculator.c -o weekday-calculator [sgupta@rhel55x86 c]$ ./weekday-calculator XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXX WEEKDAY CALCULATOR XXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Enter nYear: 2011 Enter nMonth: 9 Enter nDay: 26 Entered Day is a Monday. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX [sgupta@rhel55x86 c]$ */
See also
Other popular tricky C Sample Codes and language Concept
.
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment