How to Calculate Any Day of the Week?

Submitted by: 
Language: 
Visitors have accessed this post 1152 times.

Author : Walter
Date : September 27, 2014 Firday
MyBlog : http://learningpen.blogspot.com/
Tool : Dev C++
Language : C++
Function : Calculate any day of the week?

  1. #include <iostream>
  2. #include <cmath> // for floor()
  3. #include <conio.h> // for getch()
  4. #include <stdlib.h> // for exit()
  5. #include <windows.h> // for system()
  6.  
  7.  
  8. using std::cout;
  9. using std::cin;
  10.  
  11. //=======================
  12. void Menu( void );
  13.  
  14. int main( void )
  15. {
  16. Menu();
  17.  
  18. return 0;
  19. }// End of main function
  20.  
  21. //=======================
  22. void Menu( void )
  23. {
  24. int year, month, day;
  25. int week;
  26. int century;
  27. int temp;
  28. bool flag= false;
  29.  
  30. system( "cls" );
  31.  
  32. cout<< "\n\n =========== Calculate any day of the week =========== ";
  33. cout<< "\n\n\t\t Enter year month day: ";
  34. cin>> year>> month>> day;
  35.  
  36. temp= year;
  37.  
  38. if( year<= 1582 && month<= 10 && day<= 15 )
  39. {
  40. flag= true;
  41. }
  42.  
  43. if( month== 2 || month== 1 )
  44. {
  45. year--;
  46. month= 12+ month;
  47. }
  48.  
  49. century= year/ 100;
  50. year= year% 100; // The last two digits of the year
  51.  
  52. if( flag== false )
  53. {
  54. week= year+ ( int )floor( ( double )year/ 4 )+ ( int )floor( ( double )century/ 4 )- 2* century+
  55. ( int )floor( (double ) 26*( month+ 1 )/ 10 )+ day- 1;
  56. }
  57. else
  58. {
  59. week= year+ ( int )floor( ( double )year/ 4 )+ ( int )floor( ( double )century/ 4 )- 2* century+
  60. ( int )floor( ( double ) 26*( month+ 1 )/ 10 )+ day- 3;
  61. }
  62.  
  63. week= ( week% 7+ 7 )% 7;
  64.  
  65. year= temp;
  66.  
  67. switch( week )
  68. {
  69. case 0:
  70. {
  71. cout<< "\n\n\t\t ";
  72. cout<< year<< "-"<< month<< "-"<< day<< ": "<< "Sunday";
  73.  
  74. break;
  75. }
  76. case 1:
  77. {
  78. cout<< "\n\n\t\t ";
  79. cout<< year<< "-"<< month<< "-"<< day<< ": "<< "Monday";
  80.  
  81. break;
  82. }
  83. case 2:
  84. {
  85. cout<< "\n\n\t\t ";
  86. cout<< year<< "-"<< month<< "-"<< day<< ": "<< "Tuesday";
  87.  
  88. break;
  89. }
  90. case 3:
  91. {
  92. cout<< "\n\n\t\t ";
  93. cout<< year<< "-"<< month<< "-"<< day<< ": "<< "Wednesday";
  94.  
  95. break;
  96. }
  97. case 4:
  98. {
  99. cout<< "\n\n\t\t ";
  100. cout<< year<< "-"<< month<< "-"<< day<< ": "<< "Thursday";
  101.  
  102. break;
  103. }
  104. case 5:
  105. {
  106. cout<< "\n\n\t\t ";
  107. cout<< year<< "-"<< month<< "-"<< day<< ": "<< "Friday";
  108.  
  109. break;
  110. }
  111. case 6:
  112. {
  113. cout<< "\n\n\t\t ";
  114. cout<< year<< "-"<< month<< "-"<< day<< ": "<< "Saturday";
  115.  
  116. break;
  117. }
  118. }
  119.  
  120. cout<< "\n\n\t\t Do you want to continue( Y/N ): ";
  121.  
  122. char letter;
  123.  
  124. cin>>letter;
  125.  
  126. if( toupper( letter)== 'Y' )
  127. {
  128. Menu();
  129. }
  130. else
  131. {
  132. cout<< "\n\n\t\t Thank U.";
  133. getch();
  134.  
  135. exit( 1 );
  136. }
  137. }// End of Menu function

Tags: 

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.