Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. #define MAX_STRING_LENGTH 50
  6. #define MAX_WEEKDAYS 7
  7.  
  8. char week_days[MAX_WEEKDAYS][MAX_STRING_LENGTH] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  9.  
  10. int main(void){
  11.     int year;
  12.     printf("Enter year between 0 and 2019:\n> ");
  13.     do{
  14.         scanf("%d", &year);
  15.         if(year < 0 || year > 2019){
  16.             printf("Wrong input! Try again.\n> ");
  17.         }
  18.     } while(year < 0 || year > 2019);
  19.  
  20.     /* number of days in given year */
  21.     bool leap_year;
  22.     if (year % 400 == 0){
  23.         leap_year = true;
  24.     } else if (year % 100 == 0){
  25.         leap_year = false;
  26.     } else if (year % 4 == 0){
  27.         leap_year = true;
  28.     } else{
  29.         leap_year = false;
  30.     }
  31.  
  32.     /* 1.1.#### */
  33.     int d = 1;
  34.     int m = 1;
  35.     int y = year;
  36.  
  37.     /* 0 -> Sunday, 1 -> Monday, 2 -> Tuesday, 3 -> Wednesday, 4 -> Thursday, 5 -> Friday, 6 -> Sunday */
  38.     int first_of_january = (d += m < 3 ? y-- : y - 2, 23 * m / 9 + d + 4 + y / 4 - y / 100 + y / 400) % 7;
  39.  
  40.     printf("\nMost days in year %d: ", year);
  41.     /* non-leap year = 365 days -> most days: day on the 1st of January */
  42.     if(!leap_year){
  43.         printf("%s\n", week_days[first_of_january]);
  44.     /* leap year = 366 days -> most days: day on the 1st of January and the day after */
  45.     } else{
  46.         printf("%s and %s\n", week_days[first_of_january], week_days[(first_of_january + 1) % 7]);
  47.     }
  48.  
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement