bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / letusc / luc018-logic.c

luc018-logic.c (3276B)


      1 /* According to Gregorian calender, it was Monday on the date 01/01/01.
      2 Write a program to find out what is the day on 1st January of any input year. */
      3 /* Let Us C, Chap- 3, Page - 53, Qn No.: f(i) */
      4 
      5 #include <stdio.h>
      6 
      7 /**
      8  * @brief Determines if a given year is a leap year.
      9  * * The rule: A year is a leap year if it is divisible by 4, UNLESS it is 
     10  * divisible by 100 but NOT by 400.
     11  * * @param year The year to check.
     12  * @return 1 if it is a leap year, 0 otherwise.
     13  */
     14 int is_leap(int year) {
     15     // Check if divisible by 400 OR (divisible by 4 AND not divisible by 100)
     16     if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
     17         return 1;
     18     }
     19     return 0;
     20 }
     21 
     22 /**
     23  * @brief Calculates the day of the week for January 1st of the given year.
     24  * * The base date is 01/01/01, which was a Monday (index 1).
     25  * * Day Mapping: 0:Sunday, 1:Monday, 2:Tuesday, 3:Wednesday, 4:Thursday, 5:Friday, 6:Saturday
     26  */
     27 int main() {
     28     long long year; // Use long long for year input if years far in the future/past are tested
     29     int i;
     30     long long total_days = 0;
     31     int day_index;
     32     
     33     // Day names array for output
     34     const char *day_names[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
     35 
     36     printf("Enter the year (e.g., 2025): ");
     37     if (scanf("%lld", &year) != 1 || year < 1) {
     38         printf("Invalid year input. Please enter a positive integer year (>= 1).\n");
     39         return 1;
     40     }
     41 
     42     // --- Core Logic: Calculate Total Days ---
     43     
     44     // We only need to consider the years that have *passed* before the target year.
     45     // So, we count days from the end of year 0 up to the end of year (year - 1).
     46     int years_passed = year - 1;
     47     
     48     // 1. Calculate the number of leap days up to the end of year (year - 1)
     49     // Formula based on Gregorian calendar rules for years Y-1:
     50     // (Y-1)/4 - (Y-1)/100 + (Y-1)/400
     51     long long leap_years = years_passed / 4 - years_passed / 100 + years_passed / 400;
     52 
     53     // 2. Total days = (Number of years * 365) + (Number of leap years)
     54     // Note: The loop method (below) is more intuitive but the formula is faster.
     55     // We will use the direct formula for efficiency.
     56     total_days = years_passed * 365 + leap_years;
     57 
     58     // --- Alternate Loop Method (for conceptual simplicity) ---
     59     /*
     60     for (i = 1; i < year; i++) {
     61         total_days += 365;
     62         if (is_leap(i)) {
     63             total_days += 1; // Add 1 for the leap day
     64         }
     65     }
     66     */
     67     
     68     // --- Determine the Day of the Week ---
     69     
     70     // Since 01/01/01 was Monday (index 1), we use the following setup:
     71     // Index 1 corresponds to Monday.
     72     // The calculation gives the number of days *past* the Monday start (01/01/01).
     73     // The modulo operation gives the remainder (0-6).
     74     
     75     // 0 days elapsed (Year 1): total_days=0. (0 + 1) % 7 = 1 (Monday). Correct.
     76     // 365 days elapsed (Year 2): total_days=365. (365 + 1) % 7 = 2 (Tuesday). Correct. (365 mod 7 = 1, 1+1 = 2)
     77     
     78     day_index = (total_days + 1) % 7;
     79     
     80     // Correct the Day Index to match the array (0:Sun, 1:Mon, ..., 6:Sat)
     81     // The +1 adjusts for the Monday starting point (index 1).
     82     
     83     printf("\nOn January 1st, %lld, the day was: **%s**.\n", year, day_names[day_index]);
     84 
     85     return 0;
     86 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror